Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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" type="image/x-icon" />
<link rel="icon" href="/favicon.ico" 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 {favicon=@"(.*/)?favicon.ico(/.*)?"});

Unfortunately, IgnoreRoute does not work for Routing Web Forms though because it is not an MVC application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
399 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...