对 Web 表单使用 URL 路由,对 Favicon 使用 StopRoutingHandler [英] Using URL Routing for Web Forms and StopRoutingHandler for Favicon

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

问题描述

我有一个网站,需要在其中添加 Favicon.ico.该站点是使用 ASP.NET 3.5 Web Forms with Routing 编写的.问题是 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" />

它们在我的 标签内.

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.

推荐答案

我用过这个,效果很好:

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

显然这也有效:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆