尝试使用Request.Form获取已发布的有效负载,但Request.Form它始终为{}空?
以下是我的有效负载:

但当我尝试使用时:
var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url);我的负载Request.Form为空。
完整的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);
}发布于 2014-05-02 02:56:28
我会让事情变得简单。更改操作方法的签名:
[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.
}在这里指定MVC将对您有所帮助,因为[HttpPost]模型绑定器(负责从例如Request.Form获取数据的类)将通过将参数值的名称与Request.Form中的数据进行匹配来填充参数值。
注意,我不知道Id是数字还是字符串值。如果它是一个数字,那么你需要
[HttpPost]
public ActionResult Index(string pathInfo, int? Id, string Name, int ProjectId)
{在本例中,Id是一个可以为空的整数。
https://stackoverflow.com/questions/23410725
复制相似问题