在我們的MVC 2的應用程序之前,推進Request.InputStream我們有一個JSON模型綁定來實現,像這樣:達到ModelBinder的
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string input;
using (var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream))
{
input = reader.ReadToEnd();
}
return JsonConvert.DeserializeObject(
input,
bindingContext.ModelType);
}
更新到MVC 4後,我注意到,我們都拿到了進入的JSON職位空傳入車型。當挖掘它變得明顯的時候,上游正在推進這條河流。這很容易修復,像這樣
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string input;
//something upstream after MVC 4 upgrade is advancing the stream to end before we can read it
controllerContext.HttpContext.Request.InputStream.Position = 0;
using (var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream))
{
input = reader.ReadToEnd();
}
return JsonConvert.DeserializeObject(
input,
bindingContext.ModelType);
}
但我想知道發生了什麼事情,使更改必要?以前的實施只是巧合工作嗎?
什麼是Request.Content的價值再次推進了
Request.InputStream
到流,我們需要結束重置Request.InputStream
?這已經被填充了嗎?如果是這樣,這可能解釋爲什麼InputStream需要重新定位。 MVC4還添加了Restful WebAPI,它可以直接支持將請求正文解析爲模型參數。 – B2K 2015-02-19 15:47:05