首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Request.Form空post数据

Request.Form空post数据
EN

Stack Overflow用户
提问于 2014-05-01 23:20:43
回答 1查看 1.5K关注 0票数 0

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

以下是我的有效负载:

但当我尝试使用时:

代码语言:javascript
复制
var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url);

我的负载Request.Form为空。

完整的C#代码:

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2014-05-02 02:56:28

我会让事情变得简单。更改操作方法的签名:

代码语言:javascript
复制
[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是数字还是字符串值。如果它是一个数字,那么你需要

代码语言:javascript
复制
[HttpPost]
public ActionResult Index(string pathInfo, int? Id, string Name, int ProjectId)
{

在本例中,Id是一个可以为空的整数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23410725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档