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.HttpApplic开发者_如何学编程ation.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
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
Or if debugging you can just put the following line in the Immediate Window to check:
? HttpContext.Current.Request.Url.ToString()
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.
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
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/
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.
精彩评论