How to solve exception “File does not exist”?

邮差的信 提交于 2019-11-27 01:36:47

问题


I have a basic ASP.NET MVC2 site which logs a single "File does not exist" error every time a view (not partial views) is loaded. I am pretty sure this is because I am referencing a file, from the master page, that does not exist, but I cannot figure out which one it is.

The stack trace is not useful (see below). Does anyone have any tips on how to best debug this?

File does not exist. :    at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
   at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
   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)

回答1:


As you say, its probably a missing file or image referenced by your master page. To capture the error, add the following error handler to your Global.asax

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    if (ex.Message == "File does not exist.")
    {
        throw new Exception(string.Format("{0} {1}", ex.Message, HttpContext.Current.Request.Url.ToString()), ex);
    }
}

This should then tell you what resource the page is requesting




回答2:


Or if debugging you can just put the following line in the Immediate Window to check:

? HttpContext.Current.Request.Url.ToString()




回答3:


Check your CSS files for url( resource_path ) where "resource_path" doesn't exist.

I was getting the same exception. Found problem was in CSS file where an image file was missing from the project and not on the file system. For example a line like:

background-image: url( /images/foo.png );

Where "foo.png" file isn't in in the images folder.

You can cast the last exception as an HttpException to get the HTML status code:

HttpException httpEx = Server.GetLastError() as HttpException;
if( httpEx != null )
    httpEx.GetHttpCode();         // Will be HTML status code, 404 in this case. 

But it doesn't contain any information what file is missing. I found the missing file by checking every CSS class declaration on the page were this error was occurring.




回答4:


Add following method in Global.asax file

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    Log.Error("An error occurred", ex);
    Log.Error("Requested url: ", Request.RawUrl);
}

Request.RawUrl Give exact url which give an Error.

Now in your log file you should see:

Requested url: /favicon.ico



回答5:


In my case I was getting the same error, although I did not find any "404 errors" in the browser console. I even checked the Master file and could not find any file missing. It turned out that the browser expects favicon.ico to be at the root of the site. I created a favicon.ico at the root of my site and the issue is gone. This was a good website to generate the icon: http://www.favicon.cc/




回答6:


in my case it was a missing default home page i.e default.aspx that was the culprit.

So adding the default.aspx file solved the problem.

However, this project does not require a default page as it's not a user facing project.

It's was more of a middleware app to interface legacy systems over the web.



来源:https://stackoverflow.com/questions/3705057/how-to-solve-exception-file-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!