首页 > 解决方案 > 如何将 HttpContext.Current.Request.ReadEntityBodyMode 设置为 None?

问题描述

我正在尝试在 ASP.NET WebAPI 操作中使用流模式:

Startup.cs设置

    public static void Configuration(IAppBuilder app)
    {
        if (HostingEnvironment.IsHosted)
        {
            GlobalConfiguration.Configure(config => Configuration(app, config));
            GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new CustomWebHostBufferPolicySelector());
        }
        else
        {
            var config = new HttpConfiguration();
            Configuration(app, config);
        }
    }

处理缓冲区/流模式的类

// Try the worst case, everything has to be streamed...
public class CustomWebHostBufferPolicySelector : WebHostBufferPolicySelector
{
    // Check incoming requests and modify their buffer policy
    public override bool UseBufferedInputStream(object hostContext)
    {
        return false;
    }

    // You could also change the response behaviour too...but for this example, we are not
    // going to do anything here...
    // I override this method just to demonstrate the availability of this method.
    public override bool UseBufferedOutputStream(System.Net.Http.HttpResponseMessage response)
    {
        return base.UseBufferedOutputStream(response);
    }
}

但是在我的 WebAPI 控制器操作中,值设置为 Classic 而不是 None...

    [Route("api/v1/presentations/{presentationId:int}/documents/{documentId}")]
    public async Task<HttpResponseMessage> Post(int presentationId, string documentId)
    {
        try
        {
            var readEntityBodyMode = HttpContext.Current.Request.ReadEntityBodyMode;
            // Classic... but why?

标签: c#asp.netasp.net-web-api

解决方案


推荐阅读