在ASP.NET MVC 5中,可以使用OutputCache属性来缓存CSS和JS文件。以下是一个示例代码:
在Controller中,使用OutputCache属性来设置缓存:
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]
public ActionResult MyCssFile()
{
// 返回CSS文件内容或文件路径
return Content("", "text/css");
}
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]
public ActionResult MyJsFile()
{
// 返回JS文件内容或文件路径
return Content("", "application/javascript");
}
在View中,可以使用Url.Action方法来获取缓存的CSS和JS文件的URL:
这样,CSS和JS文件将被缓存,并从客户端缓存中加载,从而提高网页加载速度。请注意,Duration属性设置了缓存的过期时间(以秒为单位),Location属性设置了缓存的位置,这里设置为OutputCacheLocation.Client表示在客户端缓存。