Skip to content
欢迎扫码关注公众号

ASP.Net MVC 如何修复 expiration not specified 问题

使用 GTmetrix 测试网站加载速度使发现静态文件都显示 expiration not specified 的提示。

建议里只有 Apache Server 通过配置 .htaccess 文件设置过期时间的方法,ASP.Net MVC 中呢则是需要修改 Web.config 文件。

xml
<system.webServer>
    <staticContent>
        <clientCache cacheControlCustom="private" cacheControlMode="UseMaxAge" cacheControlMaxAge="10.00:00:00" />
    </staticContent>
</system.webServer>

cacheControlCustom 属性的值可以参考 Cache-Control

常用的有下面三个:

  • no-cache :不被任何容器缓存
  • private :不被代理服务器缓存,但可以被客户端缓存
  • public :可以被任意容器缓存

cacheControlMode 属性有 4 个选项:

  • NoControl :不添加 Cache-ControlExpires 到 Response Headers
  • DisableCache :添加 Cache-Control: no-cache 到 Response Headers
  • UseMaxAge :添加 Cache-Control: max-age=<nnn> 到 Response Headers
  • UseExpires :添加 Expires: <date> 到 Response Headers

cacheControlMaxAge 属性会设置 max-age 参数到 Cache-Control,其优先级高于 Expires
上面示例中设置的 10 天有效期,将添加 max-age=864000 到 Response Headers 的 Cache-Control 属性。

httpExpires 指定固定的过期时间。使用该参数需要将 cacheControlMode 设置为 UseExpires
该属性会添加一个 Expires 属性到 Response Headers 中。

值的格式必须符合 RFC 1123 规范,具体格式参考 RFC 822 - 5. DATE AND TIME SPECIFICATION
示例: Fri, 01 Jan 2010 12:00:00 GMT

参考

  1. Client Cache>
  2. Expiration not specified
  3. Cache-Control

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.