Disable chunking in Asp.Net Core

I use the Asp.Net Core Azure web application to provide a RESTful API to the client, and the client does not process the correct options.

Is it possible to completely disable Transfer-Encoding: chunkedeither at the controller level or in web.config?

EDIT: I return JsonResult something like this:

[HttpPost]
[Produces( "application/json" )]
public IActionResult Post( [FromBody] AuthRequest RequestData )
{
    AuthResult AuthResultData = new AuthResult();

    return Json( AuthResultData );
}
+8
source share
4 answers

Add response buffering ability can disable chunking. Link Microsoft.AspNetCore.Bufferingin your project and add the following line to the ConfigureStartup class method :

app.UseResponseBuffering();

See this example in the official repo

+5
source

In the ASP.NET kernel, this is similar to working with hosts:

response.Headers["Content-Encoding"] = "identity";
response.Headers["Transfer-Encoding"] = "identity";

(.. , ). , , .

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding

, :

var bufferingFeature = httpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering();
+2

.NET Core 2.0. ContentLength .

Int :

app.Use(async (ctx, next) =>
{
    var stream = new xxxResultTranslatorStream(ctx.Response.Body);
    ctx.Response.Body = stream;

    await Run(ctx, next);

    stream.Translate(ctx);
    ctx.Response.Body = stream.Stream;
});

Int xxxResultTranslatorStream:

ctx.Response.Headers.ContentLength=40;
stream.Write(writeTargetByte, 0, writeTargetByte.Length);
0

, , .Net Core 2.2

, MemoryStream, . , , IIS . , Azure, .

:

DeChunkerMiddleware {private readonly RequestDelegate _next;

    public DeChunkerMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        try
        {
            if (context.Request.Method != "OPTIONS")
            {
                var originalBodyStream = context.Response.Body;
                using (var responseBody = new MemoryStream())
                {
                    context.Response.Body = responseBody;
                    long length = 0;
                    context.Response.OnStarting(() =>
                    {
                        context.Response.Headers.ContentLength = length;
                        return Task.CompletedTask;
                    });
                    await _next(context);
                    length = context.Response.Body.Length;
                    context.Response.Body.Seek(0, SeekOrigin.Begin);
                    await responseBody.CopyToAsync(originalBodyStream);
                }
            }
            else
            {
                await _next(context);
            }

        }
        catch (Exception)
        {
        }
    }
}

Startup:

app.UseMiddleware<DeChunkerMiddleware>();

app.UseMvC().

0

All Articles