响应.AppendHeader()替换.NET Core



我在派生ActionFilterAttribute的类的OnResultExecuting()方法中使用了response.AppendHeader("Content-encoding", "gzip");。但它返回一个错误,如:

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

"HttpResponse"不包含"AppendHeader"的定义,并且找不到接受"HttpResponse’类型的第一个参数的可访问扩展方法"AppendHead"(您是否缺少using指令或程序集引用?(

ASP.NET核心响应标头使用属性来表示大多数常见标头。

要在.NET 6中设置内容编码,请使用:

response.Headers.ContentEncoding = "gzip";

对于早期版本,您需要使用Append扩展方法:

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

最新更新