1

I'm getting 404 errors as there is no /favicon.ico. The actual icon is located as /content/favicon.ico.

I've set this to my html pages:

<link rel="icon" href="@Url.Content("~/content/favicon.ico")" type="image/x-icon" />

It works, but some browsers seem to ignore it, or look for /favicon.ico anyway.

So, what I'm asking for is an ASP route that turns "/favicon.ico" into "/content/favicon.ico".

Chuck
  • 1,110
  • 3
  • 15
  • 22

2 Answers2

0

Some browsers look for favicons in the root because thats where they used to live by convention. Since routes are not used for static content like a favicon image, adding a route won't help. You could rewrite these requests using the IIS rewriting module or implement a custom handler for this specific case, but just putting the favicon in the root a lot less hassle - keep it simple.

svanelten
  • 473
  • 2
  • 14
0

I solved it by simply rewriting the target location directly in my code. Without any need of external re-write mods, as suggested on similar questions.

// Register event-listener in Web-App constructor
this.PreRequestHandlerExecute += new EventHandler(MvcApplication_PreRequestHandlerExecute);

void MvcApplication_PreRequestHandlerExecute(object sender, EventArgs e)
{
    string originalPath = HttpContext.Current.Request.Path.ToLower();
    if (originalPath == "/favicon.ico")
    {
        Context.RewritePath("~/content/favicon.ico");
    }
}

MSDN documentation: http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx

Chuck
  • 1,110
  • 3
  • 15
  • 22