我的應用程序中有太多的ActionResult,我想向他們添加gzip compersion。在沒有添加屬性的情況下向所有ActionResult添加compersion
是否有任何解決方案以避免向所有人添加屬性ActionResult?
像繼承或什麼?
這是我compersion代碼:
public class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(FilterExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding)) return;
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
同意usr的回答 - 但是,要回答你的問題,是的,你可以註冊一個全局動作過濾器。通過IIS處理gzip壓縮。 – viperguynaz
我也問你:有沒有更好的壓縮解決方案? – Raika