当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - How to fix "File does not exist" error after adding ignore route for favicon

问题描述:

I am using ASP.NET MVC 3. I am also using Autofac and MVC Extensions. The error that I initally got was:

The controller for path '/favicon.ico' was not found or does not implement IController.

I read a couple of posts and they all recommended that I put in the following:

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

Then I started to get the following error:

File does not exist.

ExceptionType:HttpException Stacktrace:

at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)

at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath)

at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)

at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

The error isn't telling me much as I don't know what what file it is trying to open up? How would I resolve this issue? I'm not using a favicon, I don't want one. I am using Internet Explorer 8.

网友答案:

The request for favicon.ico is being blindly made by most modern browsers and they are expecting a 404 (File Not Found) if there is no favicon (this is proper behaviour). Below you can find a quote from HTML5 working draft regarding Link type "icon" :

In the absence of a link with the icon keyword, for documents obtained over HTTP or HTTPS, user agents may instead attempt to fetch and use an icon with the absolute URL obtained by resolving the URL /favicon.ico against the document’s address, as if the page had declared that icon using the icon keyword.

The reason why you see the exception is that the web development server or IIS configured to use Managed/Integrated Pipeline Mode puts all requests through Global.asax (including errors).

You can try to prevent the browsers from making the requests by creating following dummy link to favicon:

<html>
    <head>
        <link rel="shortcut icon" href="#" />
        ...
    </head>
...
</html>

You can also create yourself an empty file for favicon or filter out the errors by checking HttpException.GetHttpCode() for 404 and ((System.Web.HttpApplication)Sender).Context.Request.Url for /favicon.ico.

网友答案:

If you don't want a favicon in your site but still you want to get rid of this errors, you could create a file of size 0 bytes and name it as favicon.ico.

网友答案:

alternative rule to try is just plain and simple:

// add ignore route for favicon.ico
routes.IgnoreRoute("favicon.ico");
分享给朋友:
您可能感兴趣的文章:
随机阅读: