从请求正文中获取ajax发布的数据

时间:2017-09-28 05:13:46

标签: c# asp.net-mvc

如何从请求正文中获取这些ajax发布的数据?

search=title&searchType=department+1&X-Requested-With=XMLHttpRequest

3 个答案:

答案 0 :(得分:0)

您可以将[FromBody]属性用于需要从正文中获取的参数。

Public ActionResult ([FromBody] string param1)
{
    return View();
}

希望这会有所帮助。

答案 1 :(得分:0)

您可以简单地将QueryString参数作为Controller中的参数:

Public ActionResult Index(string search, string searchType,string XRequestedWith)
{
    return View();
}

当然,这样你的参数就不能像X-Requested-With

那样破折号

您还可以使用Request.QueryString来获取QueryString。这样你就不会受到上述限制。

答案 2 :(得分:0)

如果你想从请求体中获取那些ajax发布的数据,那么第一条指令就是不将这些值放在查询字符串中,将这些值放在表单体中。如果你创建一个模态并使用这些属性传入,那就更好了阿贾克斯。

现在,您将在

等操作方法中获取这些值
Public ActionResult YourMethodName([FromBody]YourModel objYourModel)
{
   // use objYourModel to fetch your values
   return View();
}
相关问题