我试图查看通过IModelBinder界面发送的帖子中的文本。我有这样的东西:
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext.HttpContext.Request.ContentType.ToLowerInvariant().StartsWith("my special content type"))
{
var data = ???...where?应该是在帖子中发送的文本。它应该是一个文本块(我想),但我不知道如何访问它。有人能启发我吗?
发布于 2015-05-13 15:57:13
好吧,per @ScottRickman的建议,我查看了Accessing the raw http request in MVC4的文章,并了解了如何将其应用于IModelBinder:
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext.HttpContext.Request.ContentType.ToLowerInvariant().StartsWith("my special content type"))
{
var body = GetBody(controllerContext.HttpContext.Request);
var model = MyCustomConverter.Deserialize(body, bindingContext.ModelType);
return model;
}
}
private static string GetBody(HttpRequestBase request)
{
var inputStream = request.InputStream;
inputStream.Position = 0;
using (var reader = new StreamReader(inputStream))
{
var body = reader.ReadToEnd();
return body;
}
}这完全是理想的结果。
https://stackoverflow.com/questions/30214109
复制相似问题