首先,我知道我可以只使用内置的中间件(即 app.UseResponseCompression();),但我必须这样做才能支持遗留应用程序。
这是 API 在使用 System.Web.Http.Filters 的旧 .NET 框架中使用的代码:
public override void OnActionExecuted(HttpActionExecutedContext actContext)
{
HttpContext.Current.Response.Filter = new System.IO.Compression.GZipStream(HttpContext.Current.Response.Filter, System.IO.Compression.CompressionMode.Compress);
actContext.Response.Content.Headers.Add("Content-encoding", "gzip");
base.OnActionExecuted(actContext);
}
任何人都知道我如何使用 Microsoft.AspNetCore.Mvc.Filters 复制它?到目前为止,我有这个,无法弄清楚如何做实际的压缩部分。
public override async void OnActionExecuted(ActionExecutedContext context)
{
var response = context.HttpContext.Response;
//Below line doesn't work, but anywhere close?
//response.Body = new GZipOutputStream(response.Body);
response.Headers.Add("Content-Encoding", "gzip");
base.OnActionExecuted(context);
}