How to cache js and css files of ASP.NET MVC appli

2019-04-17 04:35发布

On our IIS Server 7.5 there are several ASP.NET MVC applications and in order to increase performance, we want to cache static contents (javascript, css, etc.). I have look Static Content Caching on Microsoft IIS and ASP.NET MVC caching techniques but not sure if it will fullfill my requirements. So, what is the best option for making IIS Server cache static content on MVC project side programmatically?

1条回答
放荡不羁爱自由
2楼-- · 2019-04-17 04:52

I usually use the following configuration in asp.net mvc applications to cache static content, enable gzip compression, etc.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <!-- GZip static file content.  Overrides the server default which only compresses static files over 2700 bytes -->
        <httpCompression directory="%SystemDrive%\websites\_compressed" minFileSizeForComp="1024">
            <scheme dll="%Windir%\system32\inetsrv\gzip.dll" name="gzip"/>
            <dynamicTypes>
                <add enabled="true" mimeType="text/*" />
                <add enabled="true" mimeType="message/*" />
                <add enabled="true" mimeType="application/javascript" />
                <add enabled="true" mimeType="application/json" />
                <add enabled="true" mimeType="application/json; charset=utf-8" />
                <add enabled="false" mimeType="*/*" />
            </dynamicTypes>
            <staticTypes>
                <add enabled="true" mimeType="text/*"/>
                <add enabled="true" mimeType="message/*"/>
                <add enabled="true" mimeType="application/javascript"/>
                <add enabled="true" mimeType="application/json"/>
                <add enabled="true" mimeType="application/json; charset=utf-8" />
                <add enabled="false" mimeType="*/*"/>
            </staticTypes>
        </httpCompression>
        <urlCompression doDynamicCompression="true" doStaticCompression="true"/>
        <staticContent>
            <!-- Set expire headers to 30 days for static content-->
            <clientCache cacheControlMaxAge="30.00:00:00" cacheControlMode="UseMaxAge"/>
            <remove fileExtension=".html"/>
            <mimeMap fileExtension=".html" mimeType="text/html; charset=UTF-8"/>
            <remove fileExtension=".css"/>
            <mimeMap fileExtension=".css" mimeType="text/css"/>
            <remove fileExtension=".js"/>
            <mimeMap fileExtension=".js" mimeType="text/javascript"/>
            <remove fileExtension=".json"/>
            <mimeMap fileExtension=".json" mimeType="application/json"/>
            <remove fileExtension=".map"/>
            <mimeMap fileExtension=".map" mimeType="application/json"/>
            <remove fileExtension=".rss"/>
            <mimeMap fileExtension=".rss" mimeType="application/rss+xml; charset=UTF-8"/>
            <remove fileExtension=".xml"/>
            <mimeMap fileExtension=".xml" mimeType="application/xml; charset=UTF-8"/>
            <!-- HTML5 Audio/Video mime types-->
            <remove fileExtension=".mp3"/>
            <mimeMap fileExtension=".mp3" mimeType="audio/mpeg"/>
            <remove fileExtension=".mp4"/>
            <mimeMap fileExtension=".mp4" mimeType="video/mp4"/>
            <remove fileExtension=".ogg"/>
            <mimeMap fileExtension=".ogg" mimeType="audio/ogg"/>
            <remove fileExtension=".ogv"/>
            <mimeMap fileExtension=".ogv" mimeType="video/ogg"/>
            <remove fileExtension=".webm"/>
            <mimeMap fileExtension=".webm" mimeType="video/webm"/>
            <!-- Proper svg serving. Required for svg webfonts on iPad -->
            <remove fileExtension=".svg"/>
            <mimeMap fileExtension=".svg" mimeType="image/svg+xml"/>
            <remove fileExtension=".svgz"/>
            <mimeMap fileExtension=".svgz" mimeType="image/svg+xml"/>
            <!-- HTML4 Web font mime types -->
            <!-- Remove default IIS mime type for .eot which is application/octet-stream -->
            <remove fileExtension=".eot"/>
            <mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject"/>
            <remove fileExtension=".ttf"/>
            <mimeMap fileExtension=".ttf" mimeType="application/x-font-ttf"/>
            <remove fileExtension=".ttc"/>
            <mimeMap fileExtension=".ttc" mimeType="application/x-font-ttf"/>
            <remove fileExtension=".otf"/>
            <mimeMap fileExtension=".otf" mimeType="font/opentype"/>
            <remove fileExtension=".woff"/>
            <mimeMap fileExtension=".woff" mimeType="application/font-woff"/>
            <remove fileExtension=".woff2"/>
            <mimeMap fileExtension=".woff2" mimeType="font/woff2"/>
            <remove fileExtension=".crx"/>
            <mimeMap fileExtension=".crx" mimeType="application/x-chrome-extension"/>
            <remove fileExtension=".xpi"/>
            <mimeMap fileExtension=".xpi" mimeType="application/x-xpinstall"/>
            <remove fileExtension=".safariextz"/>
            <mimeMap fileExtension=".safariextz" mimeType="application/octet-stream"/>
            <!-- Flash Video mime types-->
            <remove fileExtension=".flv"/>
            <mimeMap fileExtension=".flv" mimeType="video/x-flv"/>
            <remove fileExtension=".f4v"/>
            <mimeMap fileExtension=".f4v" mimeType="video/mp4"/>
            <!-- Assorted types -->
            <remove fileExtension=".ico"/>
            <mimeMap fileExtension=".ico" mimeType="image/x-icon"/>
            <remove fileExtension=".webp"/>
            <mimeMap fileExtension=".webp" mimeType="image/webp"/>
            <remove fileExtension=".htc"/>
            <mimeMap fileExtension=".htc" mimeType="text/x-component"/>
            <remove fileExtension=".vcf"/>
            <mimeMap fileExtension=".vcf" mimeType="text/x-vcard"/>
            <remove fileExtension=".torrent"/>
            <mimeMap fileExtension=".torrent" mimeType="application/x-bittorrent"/>
            <remove fileExtension=".cur"/>
            <mimeMap fileExtension=".cur" mimeType="image/x-icon"/>
            <remove fileExtension=".webapp"/>
            <mimeMap fileExtension=".webapp" mimeType="application/x-web-app-manifest+json; charset=UTF-8"/>
        </staticContent>
    </system.webServer>
</configuration>
查看更多
登录 后发表回答