How to solve exception "File does not exist"?
Asked Answered
G

6

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)
Granular answered 13/9, 2010 at 23:5 Comment(2)
Are you gttting a 404 for the view you're viewing? If so, use Phil Haack's Route debugger (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.Boule
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.Iq
Z
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

Zygospore answered 14/9, 2010 at 20:41 Comment(4)
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.Iq
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.Unhallowed
I am running into the same problem, did this and am now getting: File does not exist. localhost:63456Imperious
Filtering exception by english message doesn't work when operating on many different language environments. E.g. German ("Die Datei URL ist nicht vorhanden.").Pod
I
5

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

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

Investment answered 5/1, 2012 at 19:6 Comment(1)
Yep, that helped: System.Web.HttpContext.Current.Request.UrlFavorite
P
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.

Pastor answered 1/2, 2012 at 23:19 Comment(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.Protestantism
D
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
Dulcine answered 17/4, 2014 at 6:11 Comment(0)
D
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/

Demote answered 9/6, 2013 at 9:47 Comment(0)
P
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.

Pentagram answered 2/10, 2014 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.