17

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)
tereško
  • 56,151
  • 24
  • 92
  • 147
Øyvind
  • 1,580
  • 1
  • 11
  • 32
  • Are you gttting a 404 for the view you're viewing? If so, use Phil Haack's Route debugger (http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx). If that's not your problem, please show some of your code that might be relevant. – Tomas Aschan Sep 13 '10 at 23:10
  • Hello Tomas, thanks for your comment! No, I'm not getting a 404. There's an exception raised (System.Web.HttpException) that's caught by Application_Error() in my Global.asax.cs. There's no inner exception. I don't know where the exception originates from. I can post the 'logging' code from Application_Error() - but all it does is dump the exception message and stack trace to a text file. – Øyvind Sep 13 '10 at 23:25

6 Answers6

56

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

Brian Gideon
  • 45,093
  • 12
  • 98
  • 145
Clicktricity
  • 4,128
  • 1
  • 20
  • 13
  • 1
    Thanks Clicktricity! That's brilliantly simple. Makes me feel dumb for not thinking of it :-) Anyway, turns out that it's a request for favicon.ico that fails. – Øyvind Sep 15 '10 at 09:25
  • 2
    I was checking this issue for a whole evening . Thanks for your help, I got the file which is not be found:favicon.ico. Thank you very much again. – Domi.Zhang Jan 07 '12 at 16:17
  • I am running into the same problem, did this and am now getting: File does not exist. http://localhost:63456/ – Barka Apr 23 '15 at 20:25
  • Filtering exception by english message doesn't work when operating on many different language environments. E.g. German ("Die Datei URL ist nicht vorhanden."). – tibx May 25 '20 at 15:40
5

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

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

The Coder
  • 3,277
  • 1
  • 24
  • 34
2

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.

user764200
  • 41
  • 1
  • Mine was CSS as well. I am using MVC 4 and I had to move my @Scripts.Render... to the _ViewStart.cshtml (Master Layout Page), rather than _Layout.cshtml and this resolved my issue. – eaglei22 Oct 03 '16 at 19:06
1

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
Rikin Patel
  • 7,983
  • 7
  • 66
  • 73
0

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/

Has AlTaiar
  • 3,790
  • 1
  • 33
  • 36
0

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.

Oladipo Olasemo
  • 1,890
  • 22
  • 27