2

I have placed a .ico and a .png file in the folder /Content/Ico and I have the following in my _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) Is there some way for me to specify how long these favicons are cached for? Should I use some kind of web.config file inside the /Content folder?

b) Some of my code uses the syntax "<link href="@Url.Content("~/Content/ ... Should I be using @Url.Content? What's the difference between using that and just specifying /Content in the href ?

4

1 に答える 1

6

a) You could serve the favicon through a server side controller action in which you specify for how long it should be cached by decorating it with the [OutputCache] attribute:

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]
public ActionResult Favicon()
{
    var icon = Server.MapPath("~/content/ico/favicon.ico");
    return File(icon, "image/x-icon");
}

and then:

<link rel="shortcut icon" type="image/x-icon" href="@Url.Action("Favicon", "SomeController")" />

b) Always use @Url.Content("~/content/...") and never /content/... to specify relative paths to static files. The reason for that is that the Url.Content helper accounts for the virtual directory name and your site will continue to work even after you deploy it in a virtual directory in IIS. If you hardcode the url like this /content/... it will work locally but once you ship in IIS it will no longer work because now the correct location is /yourappname/content/... which is what the Url.Content helper takes into account.

于 2012-05-18T16:59:13.003 に答える