Lim
Lim

Reputation: 131

response.AppendHeader() replacement for .NET Core

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?)

Upvotes: 2

Views: 4429

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31238

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");

Upvotes: 10

Related Questions