我在文件夹/Content/Ico
中放置了一个.ico 和一个.png 文件。我的 _layout.cshtml 中有以下内容
<!-- Favicon -->
<link rel="shortcut icon" type="image/x-icon" href="/Content/Ico/favicon.ico">
<link rel="icon" type="image/png" href="/Content/Ico/tick-circle.png">
a) 我有什么方法可以指定这些网站图标的缓存时间?我应该在/Content 文件夹中使用某种 web.config 文件吗?
b) 我的一些代码使用语法 "<link href="@Url.Content("~/Content/ ...
我应该使用 @Url.Content
吗? ?使用它和只指定 /Content
有什么区别?在 href 中?
最佳答案
a) 您可以通过服务器端 Controller 操作来提供 favicon,在该操作中您可以通过使用 [OutputCache]
装饰它来指定它应该被缓存多长时间。属性:
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]
public ActionResult Favicon()
{
var icon = Server.MapPath("~/content/ico/favicon.ico");
return File(icon, "image/x-icon");
}
然后:
<link rel="shortcut icon" type="image/x-icon" href="@Url.Action("Favicon", "SomeController")" />
b) 始终使用 @Url.Content("~/content/...")
并且从不使用 /content/...
来指定静态的相对路径文件。这样做的原因是 Url.Content 帮助程序负责虚拟目录名称,即使您将站点部署到 IIS 中的虚拟目录中,您的站点仍将继续工作。如果您像这样对 url 进行硬编码 /content/...
,它将在本地工作,但是一旦您在 IIS 中发布,它将不再工作,因为现在正确的位置是 /yourappname/content/。 ..
这是 Url.Content 助手考虑的内容。
关于asp.net-mvc - 我如何安排将我的图标缓存在 MVC3 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654489/