C# 请求。表单空的post数据
试图使用Request.Form获取发布的负载,但是Request.Form总是{}空的 这是我的有效载荷: 但当我尝试使用:C# 请求。表单空的post数据,c#,asp.net-mvc,request.form,C#,Asp.net Mvc,Request.form,试图使用Request.Form获取发布的负载,但是Request.Form总是{}空的 这是我的有效载荷: 但当我尝试使用: var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url); 我的有效负载请求。表单为空 整个C代码: public ActionResult Index(string pathInfo) {
var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url);
我的有效负载请求。表单为空
整个C代码:
public ActionResult Index(string pathInfo)
{
var url = Settings.GetValue<string>("QualService") + "/" + pathInfo + "?" + Request.QueryString;
//Get stuff from the back end
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Cookie] = Request.Headers["Cookie"];
client.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(
Encoding.UTF8.GetBytes(
"x:{0}".Fmt(UserSession.ApiKey)));
try
{
var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url);
var result = new ContentResult();
result.Content = Encoding.UTF8.GetString(responseBytes);
result.ContentEncoding = Encoding.UTF8;
result.ContentType = "application/json";
return result;
}
catch(Exception e)
{
Logger.Error("Error while proxying to the API: ", e);
}
}
return Json(false);
}
我会保持简单。更改操作方法的签名:
[HttpPost]
public ActionResult Index(string pathInfo, string Id, string Name, int ProjectId)
{
// ID, Name and ProjectId will be populated with the values in the payload.
// ... Rest of code.
}
指定[HttpPost]将对您有所帮助,因为MVC模型绑定器是负责从例如Request.Form中获取数据的类,它将通过将参数值的名称与Request.Form中的数据匹配来填充参数值
注意,我不知道Id是数字还是字符串值。如果是一个数字,那么你需要
[HttpPost]
public ActionResult Index(string pathInfo, int? Id, string Name, int ProjectId)
{
在本例中,Id是一个可为空的整数。我想我找到了问题所在。Application/json是post。您如何通过$.ajax发出post请求?您可能需要更改contentType值以满足您的需要。$http angular它会自动为您提供contentType应用程序/json您可以发布angular$http代码的代码吗?