How to solve exception “File does not exist”?

空扰寡人 提交于 2019-11-28 06:55:13
Clicktricity

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.

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