使用Web窗体的URL路由和Favicon的StopRoutingHandler

Ben*_*man 9 c# asp.net routing

我有一个网站,我需要添加一个Favicon.ico.该站点使用带有路由的ASP.NET 3.5 Web窗体编写.问题是Favicon链接始终返回页面未找到错误.这是因为路由不知道Favicon.ico的链接应该去哪里,所以它返回Not Found页面.

我试图为favicon添加一个StopRoutingHandler,但它们似乎都没有用.以下是我到目前为止尝试过的内容:

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()));
Run Code Online (Sandbox Code Playgroud)

有谁知道我应该使用什么?我尝试过的favicon.ico链接看起来像这样:

<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
Run Code Online (Sandbox Code Playgroud)

它们在我的<html><head>标签内.

另外,作为最后一点,我不使用MVC,因为如果我是,我可以使用它:

routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});
Run Code Online (Sandbox Code Playgroud)

遗憾的是,IgnoreRoute不适用于Routing Web Forms,因为它不是MVC应用程序.

Ben*_*man 5

我用了它,它起作用了:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

显然这也可行:

routes.Add(new Route("favicon.ico", new StopRoutingHandler()));
Run Code Online (Sandbox Code Playgroud)

我只需要关闭Firefox,清除历史记录,然后重试即可。