How to implement GZip compression in ASP.NET?

后端 未结 9 827
后悔当初
后悔当初 2020-11-27 10:21

I am trying to implement GZip compression for my asp.net page (including my CSS and JS files). I tried the following code, but it only compresses my .aspx page (found it fro

相关标签:
9条回答
  • 2020-11-27 10:57

    For compressing JS & CSS files you actually have to handle that at the IIS level, since these files are rendered directly without the ASP.NET runtime.

    You could make a JSX & CSSX extension mapping in IIS to the aspnet_isapi.dll and then take advantage of your zip code, but IIS is likely to do a better job of this for you.

    The content-encoding header tells the browser that it needs to unzip the content before rendering. Some browsers are smart enough to figure this out anyway, based on the shape of the content, but it's better to just tell it.

    The Accept-encoding cache setting is there so that a cached version of the gzipped content won't be sent to a browser that requested only text/html.

    0 讨论(0)
  • 2020-11-27 10:59

    In IIS7 all requests go to .net, so you would have to create an HttpModule that added those headers to all responses.

    Without IIS7, and on shared hosting, you would have to creare a handler that mapped a .net file extention that you are not using (like .asmx) and in the web.config specify that .asmx files go to your HttpHandler which is set to rewrite the path to .jpg or whatever and set the header there too.

    0 讨论(0)
  • To answer your last question. Those two lines set HTTP headers for the response sent back to the browser. Content-Encoding tells the browser that the response is encoded as gzip and it needs to be decoded. The last line adds Accept-Encoding to the Vary header. With this the browser or proxies can determine whether the response was unique or is influenced by certain other headers and adjust their caching.

    0 讨论(0)
提交回复
热议问题