Home > OS >  response.AppendHeader() replacement for .NET Core
response.AppendHeader() replacement for .NET Core

Time:02-16

I have used response.AppendHeader("Content-encoding", "gzip"); inside a OnResultExecuting() method of a class that derives ActionFilterAttribute. But it returns an error like:

//HttpResponseBase response = filterContext.HttpContext.Response;
HttpResponse response = filterContext.HttpContext.Response;
response.AppendHeader("Content-encoding", "gzip");

'HttpResponse' does not contain a definition for 'AppendHeader' and no accessible extension method 'AppendHeader' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?)

CodePudding user response:

The ASP.NET Core response headers use properties to represent most of the common headers.

To set the content encoding in .NET 6, use:

response.Headers.ContentEncoding = "gzip";

For earlier versions, you'll need to use the Append extension method:

response.Headers.Append("Content-Encoding", "gzip");

CodePudding user response:

Sounds like what you need is to use this like a static method:

Response.AppendHeader("Content-encoding", "gzip");

https://docs.microsoft.com/en-us/dotnet/api/system.web.httpresponse.appendheader?view=netframework-4.8

  • Related