gpt4 book ai didi

c# - 当 Content-Type 为 text/plain 时,如何让 .NET MVC 绑定(bind) POST 参数

转载 作者:行者123 更新时间:2023-11-30 14:58:02 26 4
gpt4 key购买 nike

我有一个 IE8/IE9 CORS request using XDomainRequest POST 类型进入 ASP .NET MVC 3 Web 应用程序。链接的博客文章表明:

Only text/plain is supported for the request's Content-Type header

现在,由于 Content-Type 是 text/plain 而这超出了我的控制范围,MVC 框架将不会绑定(bind) POST 内容参数。它似乎只在 Content-Typeapplication/x-www-form-urlencoded 时绑定(bind)它们。

我也无法从 Request 对象中读取参数。

我需要找到一种方法让 MVC 绑定(bind)这些参数。我考虑过尝试修改 Application_BeginRequest 请求的 Content-Type,但这没有用。

Content-Typetext/plain 时,如何让 MVC 框架绑定(bind) POST 参数?


更新

我相信这些参数可以通过 Request.InputStream 获得。属性(property)。我正在寻找一种使用 MVC 框架默认绑定(bind)来一般绑定(bind)这些参数的方法。我不想为项目中的每个模型都编写模型绑定(bind)器。

最佳答案

检查是否:

  • 您确实在发送 x-www-form-urlencoded。也许您要发送 JSON?
  • 响应包含 Access-Control-Allow-Origin header

我尝试了以下方法并且有效:

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_BeginRequest()
{
if (String.IsNullOrEmpty(Context.Request.ContentType) && Context.Request.HttpMethod == "POST")
{
Context.Request.ContentType = "application/x-www-form-urlencoded";
}
}

protected void Application_EndRequest()
{
Context.Response.AddHeader("Access-Control-Allow-Origin", "*");
}
}

注意:为了更好的模块化,您可能希望将 Application_BeginRequest 实现为 HTTP 模块,并将 CORS(Access-Control-Allow-Origin)实现为 ActionFilter

关于c# - 当 Content-Type 为 text/plain 时,如何让 .NET MVC 绑定(bind) POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19868295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com