我编写了非常简单的缩小/压缩处理程序,通过指示请求类型(Request.RawUrl.EndsWith("css" || "js")
)来缩小css和js,但我不知道一种方法来指示哪个响应类型是html然后将其缩小为HTML内容,因为在mvc中不是检查的扩展。
提前感谢;)
答案 0 :(得分:1)
如果您编写HTTP handler来压缩静态资源,则可以根据文件类型设置Content-Type
标头:
if (Request.RawUrl.EndsWith("css"))
{
Response.ContentType = "text/css";
}
else if (Request.RawUrl.EndsWith("js"))
{
Response.ContentType = "text/javascript";
}
顺便说一下,我建议您提前缩小/压缩静态资源,并依赖Web服务器的gzip
压缩和客户端缓存。如果不是教育目的,我会避免写这样的处理程序。
答案 1 :(得分:0)
public void ProcessRequest(HttpContext context)
{
if (Preferences.EnableHtmlMinification && **IfResponseContentIsHtml**)
{
//Do minify here
}
if (Preferences.EnableHtmlCompression && **IfResponseContentIsHtml**)
{
acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);
if (acceptEncoding.Contains("gzip"))
{
response.Filter = new HtmlCompressStream(response.Filter, CompressionMode.Compress, HtmlCompressStream.CompressionType.GZip);
response.AddHeader("Content-encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
response.Filter = new HtmlCompressStream(response.Filter, CompressionMode.Compress, HtmlCompressStream.CompressionType.Deflate);
response.AddHeader("Content-encoding", "deflate");
}
}
else
{
response.Filter = new HtmlCompressStream(response.Filter, CompressionMode.Compress, HtmlCompressStream.CompressionType.None);
}
}