Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1740
  • Last Modified:

Finding missing filename on "File Does Not Exist" exception

I catch the exception in the Global.asax.Application_Error() function, and extract the
exception, log it and redirect to the proper page thusly: (I use MVC)

        protected void Application_Error(object sender, EventArgs e)
        {
                Exception exception = Server.GetLastError();
                LogException(exception);
                Response.Clear();
                Server.ClearError();  
                Response.Redirect(String.Format("/Error/Generic/?outputStr={0}", exception.Message));
        }

BUT I CAN NOT FIND THE FILENAME that's missing.

I have browsed Google and found this post, which was never answered completely.
http://www.codeguru.com/forum/showthread.php?t=508023

I do not want to install some program to find the filename, as was suggested. It's in either the exceptionor Server, since I saw it once while debugging. But I was to grab the filename and log that to the exception log file.

Can anyone tell me how to find it?

Thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
  • 10
  • 7
7 Solutions
 
newbiewebSr. Software EngineerAuthor Commented:
I tried this without success:

FileNotFoundException fnfException = exception as FileNotFoundException;
and
FileNotFoundException fnfException = (FileNotFoundException)ex;

so I could do this:
string fileNameStr = fnfException.FileName;

but got an exception in both cases when I tried to typecast to FileNotFoundException
 
Is there a proper way to do this?

newbieweb
0
 
newbiewebSr. Software EngineerAuthor Commented:
and the InnerException is null.

I think I got the new exception when I ran this line with a null fnfException, not when I typecasted it, as I has said on my previous post.

string fileNameStr = fnfException.FileName;
0
 
wdosanjosCommented:
Try this:

string filename = null;

for (var ex = exception; ex != null; ex = e.InnerException)
{
	FileNotFoundException fex = ex as FileNotFoundException;
	if (fex != null)
	{
		filename = fex.FileName;
		break;
	}
}

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
wdosanjosCommented:
Sorry, I had a typo:

string filename = null;

for (var ex = exception; ex != null; ex = ex.InnerException)
{
    FileNotFoundException fex = ex as FileNotFoundException;
    if (fex != null)
    {
        filename = fex.FileName;
        break;
    }
}

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
Your code works fine, but the value for fex is still null. So the filename remains null. This must not be a FileNotFoundException.
0
 
newbiewebSr. Software EngineerAuthor Commented:
In the code I'm using to throw the exception, it's JQuery looking for the file (which I intentionally renamed) and the Exception gets pulled out in the Application_Error() function via the following call:

Exception exception = Server.GetLastError();
0
 
wdosanjosCommented:
The code goes through the exception stack searching for a FileNotFoundException.  If after the loop filename is still null, it means that there is no FileNotFoundException on the exception stack.  Can you post the exception.ToString() value?
0
 
newbiewebSr. Software EngineerAuthor Commented:
I added this line:
string expStr = exception.ToString();

but on the next line, I see nothing whenI put the mouse of expStr. When I put the mouse over exceptionI see

{"File does not exist."}

That tells me the expStr is null or (more likely) undefined.
0
 
newbiewebSr. Software EngineerAuthor Commented:
We also have these available:

object sender, EventArgs e

those are the inputs into Global.asax.Application_Error(), as you can see in the original post.

newbieweb
0
 
wdosanjosCommented:
If you are not using 'expStr' in any other line, the compiler might optimize the code and remove the variable and the assignment.  Please the check the content of exception.ToString() under 'Quick Watch'.  If that still returns null, then check exception.GetType().Name and exception.StackTrace.
0
 
newbiewebSr. Software EngineerAuthor Commented:
Undefined. See attached...
Exception.PNG
0
 
wdosanjosCommented:
Please add the line #2 below and check if you can see expStr's value.

string expStr = exception.ToString();
if (expStr != null) Console.WriteLine(expStr);

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
I got a value:

System.Web.HttpException (0x80004005): File does not exist.
   at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
   at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context, String overrideVirtualPath)
   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)
0
 
wdosanjosCommented:
I found this post addressing to your problem.  The 'File does not exist' exception is related to a missing resource (image, css, js).

     http://dotbert.loedeman.nl/httpexception-file-does-not-exist

Try changing Application_Error as follows:

protected void Application_Error(object sender, EventArgs e)
{
    HttpApplication app = sender as HttpApplication;
    Exception exception = Server.GetLastError();
    if (app != null) exception.Data.Add("Url", app.Context.Request.Url);
    LogException(exception);
    Response.Clear();
    Server.ClearError();  
    Response.Redirect(String.Format("/Error/Generic/?outputStr={0}", exception.Message));
}

Open in new window


Then your LogException could be something like this:

private void LogException(Exception exception)
{
    if (exception != null)
    {
        string url = (exception.Data.Contains("Url")) 
                   ? exception.Data["Url"].ToString() 
                   : "";
        // url now contains the URL that caused the exception
        // most likely it points to the missing resource file

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
This does get added:

exception.Data.Add("Url", app.Context.Request.Url);

I broke this up to see exactly what wass happening:
string url = null;
if ( exception.Data.Contains("Url"))
{
     url = exception.Data["Url"].ToString();
}

but the value for url is not defined, like outputStr was before.
0
 
wdosanjosCommented:
You need to use 'url' in another line of code.  You can try this:

string url = null;
if ( exception.Data.Contains("Url"))
{
     url = exception.Data["Url"].ToString();
     Console.WriteLine(url);
}



0
 
newbiewebSr. Software EngineerAuthor Commented:
THANKS! THIS WORKS!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now