【问题标题】:HttpContext.Request.Form does not contain posted values in MVCHttpContext.Request.Form 不包含 MVC 中的发布值
【发布时间】:2016-01-11 09:22:46
【问题描述】:

以下是我如何将数据发布到 MVC 中的控制器操作的示例。数据完美地填充到我的模型对象中并且工作正常。

var data={};
data.SearchText = 'abc'
data.SearchText1 = 'abcd'
var contentType = 'application/json; charset=utf-8';

$.ajax({
      type: 'POST',
      cache: false,
      contentType: contentType,
      url: User/_UserList,
      data: JSON.stringify(data),
      success: successHandlerFunction,
      complete: completeHandlerFunction
});

[HttpPost]
public JsonResult _UserList(SearchViewModel model)
{
    var  users= "" ; //Get Data from DB ;
    return Json(users, JsonRequestBehavior.AllowGet);
}

但如果出现异常,我会尝试获取模型的值

public void OnException(ExceptionContext filterContext)
{


//filterContext.HttpContext.Request.Form -- is not giving any value
//filterContext.HttpContext.Request.QueryString -- is also having no value
//filterContext.HttpContext.Request.Params -- also no value about model

}

谁能告诉我为什么上面的代码不起作用

【问题讨论】:

  • 你有例外吗?
  • 你的模型全局变量在课堂上吗?
  • 不应该是“protected override void OnException”吗?
  • 嗨,Voo,OnException 是 IExceptionFilter 接口的实现。
  • @Mukund,model 是 _UserList 动作的参数名称,@Rahul 我没有得到任何异常

标签: c# asp.net-mvc asp.net-mvc-4 httprequest


【解决方案1】:

抱歉 - 无法评论您的答案,因此请提供新答案。 解决方案是正确的 - 您应该将 contentType 设置为 'application/x-www-form-urlencoded'。原因在这里描述得很清楚:HttpRequest.Form Property

当 HTTP 请求 Content-Type 值为“application/x-www-form-urlencoded”或“multipart/form-data”时,会填充 Form 属性。

至于 'JSON.stringify(data)' - 实际上会从字典中生成一个字符串,在这种情况下 HttpRequest.Form 无法填充,因为它需要字典,基本上,只是 'data'。

【讨论】:

  • 是的,我知道了。但只是想知道我们是否使用 contentType 'application/json; 发布数据; charset=utf-8' ,那么如何从 Request 对象中获取发布的数据,或者有没有其他方法可以检索发布的数据
  • 我尝试了以下函数来提取 JSON 数据,但仍然无法从请求对象中获取 json 数据。私有字符串 GetJsonContents(System.Web.HttpRequestBase 请求) { 字符串 JsonContents; using (Stream receiveStream = Request.InputStream) { using (StreamReader readStream = new StreamReader(receiveStream)) { JsonContents = readStream.ReadToEnd(); } } 返回 JsonContents; }
  • 但是为什么需要从原始 HttpRequest 中提取 Json 数据呢?特别是,如果我们谈论的是 ASP.NET MVC。也许使用this之类的东西会更好?
【解决方案2】:

通过从 'application/json; 更改 contentType; charset=utf-8' 到 'application/x-www-form-urlencoded' HttpContext.Request.Form 开始填充值。我们还需要在传递数据时删除 JSON.stringify。

如果我们使用 contentType 'application/json; 发布数据; charset=utf-8' 。我们需要从请求对象中检索 Json 内容,然后我们可以使用以下代码

 private string GetJsonContents(System.Web.HttpRequestBase Request)
    {
        string JsonContents = string.Empty;
        try
        {
            using (Stream receiveStream = Request.InputStream)
            {
                using (StreamReader readStream = new StreamReader(receiveStream))
                {
                    receiveStream.Seek(0, System.IO.SeekOrigin.Begin);
                    JsonContents = readStream.ReadToEnd();
                }
            }
        }
        catch
        {
        }
        return JsonContents;
    }

那么我们可以使用下面的代码来获取JSON Object

 string requestData = GetJsonContents(HttpContext.Current.Request);
 dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(requestData);//using Newtonsoft dll

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-07
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多