简体   繁体   中英

Using URL Routing for Web Forms and StopRoutingHandler for Favicon

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM