問題描述
我有一個需要添加 Favicon.ico 的網(wǎng)站.該站點是使用帶有路由的 ASP.NET 3.5 Web 窗體編寫的.問題是 Favicon 鏈接總是返回一個找不到頁面的錯誤.這是因為路由不知道 Favicon.ico 的鏈接應(yīng)該去哪里,所以它返回 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.
我嘗試為網(wǎng)站圖標(biāo)添加 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()));
有人知道我應(yīng)該使用什么嗎?我嘗試過的 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>
標(biāo)簽內(nèi).
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 應(yīng)用程序.
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()));
我只需要關(guān)閉 Firefox,清除我的歷史記錄,然后重試.
I just needed to close Firefox, clear my history and try again.
這篇關(guān)于為 Web 表單使用 URL 路由,為 Favicon 使用 StopRoutingHandler的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!