C# IModelBinder:访问请求的原始数据
我试图通过C# IModelBinder:访问请求的原始数据,c#,asp.net-mvc,asp.net-mvc-5,model-binding,C#,Asp.net Mvc,Asp.net Mvc 5,Model Binding,我试图通过IModelBinder界面查看帖子中发送的文本。我有点像: public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext.HttpContext.Request.ContentType.ToLowerInvariant().StartsWith("my special con
IModelBinder
界面查看帖子中发送的文本。我有点像:
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (controllerContext.HttpContext.Request.ContentType.ToLowerInvariant().StartsWith("my special content type"))
{
var data = ???
…在哪里???应该是邮件中发送的文本。它应该是一个文本块(我想),但我不知道如何访问它。有人能给我点化一下吗?好的,根据@ScottRickman的建议,我看了上的文章,并了解了如何将其应用于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;
}
}
这完全符合要求。您可以使用
ModelBindingContext
,例如,如果您希望从名为LastName
的表单控件中获取值,则var LastName=GetValue(bindingContext,“LastName”),
我不想要“LastName”。。。我要整个玉米饼。我怎样才能看到发送回来的全部文本?也许您可以使用这个SO问题中的方法来读取原始http请求?