首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IModelBinder:访问请求的原始数据

IModelBinder:访问请求的原始数据
EN

Stack Overflow用户
提问于 2015-05-13 11:57:02
回答 1查看 1.4K关注 0票数 4

我试图查看通过IModelBinder界面发送的帖子中的文本。我有这样的东西:

代码语言:javascript
复制
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext.HttpContext.Request.ContentType.ToLowerInvariant().StartsWith("my special content type"))
        {
            var data = ???

...where?应该是在帖子中发送的文本。它应该是一个文本块(我想),但我不知道如何访问它。有人能启发我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-13 15:57:13

好吧,per @ScottRickman的建议,我查看了Accessing the raw http request in MVC4的文章,并了解了如何将其应用于IModelBinder:

代码语言:javascript
复制
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;
    }
}

这完全是理想的结果。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30214109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档