如何在asp.net mvc中检测响应内容类型

发布于 2024-09-06 11:47:23 字数 175 浏览 6 评论 0原文

我编写了非常简单的缩小/压缩处理程序,通过指示请求类型(Request.RawUrl.EndsWith("css" || "js"))来缩小 css 和 js,但我不知道一种指示哪种响应类型是 html 的方法,然后将其缩小为 HTML 内容,因为在 mvc 中不是检查的扩展。

提前致谢 ;)

I've wrote very simple minification/compression handler that minify css and js by indicating request type (Request.RawUrl.EndsWith("css" || "js")), but i don't know an approach to indicate which response type is html and then minify that as HTML-content because in mvc isn't extension to checking.

thanks in advance ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-09-13 11:47:23

如果您编写了 HTTP 处理程序来压缩静态资源您可以根据文件类型设置 Content-Type 标头:

if (Request.RawUrl.EndsWith("css"))
{
    Response.ContentType = "text/css";
} 
else if (Request.RawUrl.EndsWith("js"))
{
    Response.ContentType = "text/javascript";
}

顺便说一句,我建议您提前缩小/压缩静态资源并依赖网络服务器的 gzip< /code> 压缩和客户端缓存。如果不是出于教育目的,我会避免编写此类处理程序。

If you wrote an HTTP handler to compress static resources it's up to you to set up the Content-Type header based on the file type:

if (Request.RawUrl.EndsWith("css"))
{
    Response.ContentType = "text/css";
} 
else if (Request.RawUrl.EndsWith("js"))
{
    Response.ContentType = "text/javascript";
}

Btw I would recommend you to minify/compress your static resources in advance and rely on the web server's gzip compression and client caching. I would avoid writing such handlers if it's not education purposes.

清音悠歌 2024-09-13 11:47:23

嗯...,我认为你误解了我的目标/问题。这是我的处理程序:

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

hm..., i think you misunderstood my goal/problem. here are my handler:

 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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文