I have a website where I need to add a Favicon.ico. The site is written using ASP.NET 3.5 Web Forms with Routing. The issue is that the Favicon link always returns a page not found error. This is because the Routing does not know where the link for Favicon.ico should go to so it returns the Not Found page.
I have tried to add a StopRoutingHandler for the the favicon but none of them seem to work. Below are the ones I have tried so far:
routes.Add(new Route("MasterPages/{favicon}.ico", new StopRoutingHandler()));
routes.Add(new Route("{favicon}.ico", new StopRoutingHandler()));
routes.Add(new Route("favicon.ico", new StopRoutingHandler()));
routes.Add(new Route("favicon.ico/{*pathInfo}", new StopRoutingHandler()));
Does anyone know what I should be using? My favicon.ico links I have tried look like this:
<link rel="shortcut icon" href="/favicon.ico" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" type="image/x-icon" /> <link rel="icon" href="/favicon.ico" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" type="image/x-icon" />
And they are inside of my <html><head> tags.
Also, as one final note, I am not using MVC because if I was I could use this:
routes.IgnoreRoute("{*favicon}", new {<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a7c7b6c73797574275a">[email protected]</a>"(.*/)?favicon.ico(/.*)?"});
Unfortunately, IgnoreRoute does not work for Routing Web Forms though because it is not an MVC application.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I used this and it worked:
routes.Add(new Route("favicon.ico", new StaticFileRouteHandler("~/favicon.ico")));
public class StaticFileRouteHandler : IRouteHandler
{
public string VirtualPath { get; set; }
public StaticFileRouteHandler(string virtualPath)
{
VirtualPath = virtualPath;
}
public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
{
HttpContext.Current.RewritePath(VirtualPath);
return new DefaultHttpHandler();
}
}
Apparently this works too:
routes.Add(new Route("favicon.ico", new StopRoutingHandler()));
I just needed to close Firefox, clear my history and try again.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0