asp.net-mvc - 我如何安排将我的图标缓存在 MVC3 中?

标签 asp.net-mvc asp.net-mvc-3

我在文件夹/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/

相关文章:

c# - 访问当前的 RouteContext

c# - 如何使用 AutoMapper 将列表映射到 ASP.NET MVC 中的 SelectList?

asp.net-mvc - ASP.NET MVC 2-Html.DropDownList用于与ViewModel混淆

asp.net-mvc-3 - t4mvc,无法解析符号 "MVC"

c# - 我的代码如何确定它是否在 IIS 中运行?

asp.net-mvc - MVC4 : Manually set the validation message from the server side

c# - 构建一个 self 维护的集合,在项目过期时将其删除?

使用 RouteBase 的 C# MVC 路由

c# - FluentValidation 和服务器+客户端远程验证器

c# - system.serviceModel/bindings/wsHttpBinding 处的绑定(bind)没有已配置的绑定(bind)