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

问题描述

我有一个网站,我需要添加的favicon.ico。该网站使用的路由与ASP.NET 3.5 Web窗体写的。问题是,该网站图标链接总是返回错误未找到页面。这是因为路由不知道是哪里的链接的favicon.ico应该去这样它会返回页面未找到。

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

和他们的内部我的&LT; HTML和GT;&LT;头方式&gt; 标签

此外,作为最后一个音符,我不使用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();
    }
}

显然,这也工作:

Apparently this works too:

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

我只是需要关闭Firefox,清除我的历史,然后再试一次。

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

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

10-20 20:08