Dan*_* T. 11 c# asp.net-mvc httpwebrequest modelbinders asp.net-mvc-3
在ASP.NET MVC 3 RC2中,默认的ModelBinder将自动解析请求正文(如果Content-Type设置为)application/json.问题是,这留下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();
Run Code Online (Sandbox Code Playgroud)
因为我正在使用Json.NET序列化/反序列化,所以我想禁用默认的ModelBinder进行额外的解析.有没有办法做到这一点?
Bri*_*all 15
您可以在Global.asax中的Application_Start中添加以下内容:
ValueProviderFactories.Factories.Remove(
ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().First());
Run Code Online (Sandbox Code Playgroud)
这假设只有一种类型(默认情况下),但如果有多种类型,可以轻松更改.如果那就是你想要的,我不相信会有一种更清洁的方式.
| 归档时间: |
|
| 查看次数: |
1651 次 |
| 最近记录: |