ASP.NET MVC ySlow为动态提供的脚本添加Expires Headers

时间:2012-10-11 04:19:25

标签: asp.net-mvc yslow controller-action expires-header

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

  [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属性来自here

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

1 个答案:

答案 0 :(得分: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;