本文介绍了为 Web 表单使用 URL 路由,为 Favicon 使用 StopRoutingHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试为网站图标添加 StopRoutingHandler,但它们似乎都不起作用.以下是我迄今为止尝试过的:

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

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

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" />

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

And they are inside of my <html><head> tags.

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

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(/.*)?"});

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

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

推荐答案

我用过,效果不错:

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,清除我的历史记录,然后重试.

I just needed to close Firefox, clear my history and try again.

这篇关于为 Web 表单使用 URL 路由,为 Favicon 使用 StopRoutingHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:45