1

我正在为 Javascript 提供一个如下所示的控制器方法:

  [OutputCache(Duration = 120)]
  [Compress]
  public ActionResult JavascriptFile(String scriptName) {
     string content;
     if (HttpContext.IsDebuggingEnabled) {
        content = ReadApplicationScript(string.Format("~/scripts/{0}", scriptName));
        return Content(content, "application/javascript");
     }
     else {
        content = ReadApplicationScript(string.Format("~/scripts/Built/{0}", scriptName));
        return Content(content, "application/javascript");
     }
  }

Compress属性来自这里

当我运行 ySlow 时,我在“添加过期标题”上获得了 F 级。我能做些什么来添加这些?

4

1 回答 1

2

在 system.webServer 部分添加以下内容

<staticContent>
      <clientCache cacheControlMode="UseExpires"
         httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />
    </staticContent>

这里的 httpExpires 值将是到期日期。

编辑

您还可以尝试将内容添加到缓存,如下所示:

var cacheName = "someName";
var value = HttpRuntime.Cache.Get(cacheName) as ContentResult;

            if (value == null)
            {                
                var contentResult = ReadApplicationScript(string.Format("~/scripts/{0}",scriptName));
                System.Web.HttpContext.Current.Cache.Insert(cacheName, contentResult );
                return contentResult;
            }

            return value;
于 2012-10-11T04:23:52.497 回答