Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of the HttpResponseBase.BufferOutput property in ASP.NET Core 2?

Tags:

asp.net-core

I am in the process of converting a large .NET Framework project to a .NET Core project, and I have come across the following code:

public class ContentStreamingResult : ActionResult {

    private Action<Stream> _onExecuteAction;

    public ContentStreamingResult(Action<Stream> onExecuteAction) {
        _onExecuteAction = onExecuteAction;
    }

    public override void ExecuteResult(ControllerContext context) {
        var httpContext = context.HttpContext;
        httpContext.Response.BufferOutput = false;
        _onExecuteAction(httpContext.Response.OutputStream);
    }
}

There is no BufferOutput property on the HttpResponse class in .NET Core.

What is the equivalent of the HttpResponseBase.BufferOutput property in ASP.NET Core 2?

like image 364
tacos_tacos_tacos Avatar asked Sep 27 '18 17:09

tacos_tacos_tacos


2 Answers

For enabling Buffering in Asp.Net Core, you could use UseResponseBuffering middleware in Startup like below:

app.UseResponseBuffering();

After applying the Buffering Middleware, if you want to disable buffer for specific requests, you could try code below:

var bufferingFeature = httpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering();
like image 166
Edward Avatar answered Sep 20 '22 06:09

Edward


You can use IHttpResponseBodyFeature for response buffer or IHttpRequestBodyFeature for request buffer.

 httpContext.Features.Get<IHttpResponseBodyFeature>().DisableBuffering()
like image 27
Ehsan Vali Avatar answered Sep 21 '22 06:09

Ehsan Vali