c# – 有没有办法在ASP.NET MVC 3 RC2中禁用JSON ModelBinder?
发布时间:2020-12-15 06:57:08 所属栏目:百科 来源:网络整理
导读:在ASP.NET MVC 3 RC2中,如果Content-Type设置为application / json,默认的ModelBinder将自动解析请求主体.问题是,这将留下流的结尾的Request.InputStream.这意味着如果您尝试使用自己的代码读取输入流,则首先将其重新设置为开始: // client sends HTTP requ
在ASP.NET MVC 3 RC2中,如果Content-Type设置为application / json,默认的ModelBinder将自动解析请求主体.问题是,这将留下流的结尾的Request.InputStream.这意味着如果您尝试使用自己的代码读取输入流,则首先将其重新设置为开始:
// client sends HTTP request with Content-Type: application/json and a JSON // string in the body // requestBody is null because the stream is already at the end var requestBody = new StreamReader(Request.InputStream).ReadToEnd(); // resets the position back to the beginning of the input stream var reader = new StreamReader(Request.InputStream); reader.BaseStream.Position = 0; var requestBody = reader.ReadToEnd(); 由于我正在使用Json.NET进行序列化/反序列化,所以我想禁用默认的ModelBinder进行额外的解析.有什么办法吗? 解决方法
您可以在Global.asax中的Application_Start中放入以下内容:
ValueProviderFactories.Factories.Remove( ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().First()); 这假设只有一种类型(默认情况下是),但是如果有多种类型,则可以很容易地将其更改为工作.如果这是你正在寻找的,我不相信有一个更干净的方式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |