【问题标题】:Using URL Routing for Web Forms and StopRoutingHandler for Favicon为 Web 表单使用 URL 路由,为 Favicon 使用 StopRoutingHandler
【发布时间】:2011-01-30 20:10:33
【问题描述】:

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

我尝试为网站图标添加 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()));

有人知道我应该使用什么吗?我尝试过的 favicon.ico 链接如下所示:

<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />

它们在我的&lt;html&gt;&lt;head&gt; 标签内。

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

routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});

很遗憾,IgnoreRoute 不适用于路由 Web 表单,因为它不是 MVC 应用程序。

【问题讨论】:

    标签: c# asp.net routing


    【解决方案1】:

    我用过这个,效果很好:

    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();
        }
    }
    

    显然这也有效:

    routes.Add(new Route("favicon.ico", new StopRoutingHandler()));
    

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

    【讨论】:

    • 这也有帮助;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2015-10-09
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多