Solved

Finding missing filename on "File Does Not Exist" exception

Posted on 2011-03-02
17
1,531 Views
Last Modified: 2012-05-11
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
Comment
Question by:newbieweb
  • 10
  • 7
17 Comments
 

Author Comment

by:newbieweb
ID: 35018235
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
 

Author Comment

by:newbieweb
ID: 35018265
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
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35018361
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
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35018379
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
 

Author Comment

by:newbieweb
ID: 35018960
Your code works fine, but the value for fex is still null. So the filename remains null. This must not be a FileNotFoundException.
0
 

Author Comment

by:newbieweb
ID: 35019079
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
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35019124
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
 

Author Comment

by:newbieweb
ID: 35019306
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:newbieweb
ID: 35019359
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
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35019371
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
 

Author Comment

by:newbieweb
ID: 35019423
Undefined. See attached...
Exception.PNG
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35019742
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
 

Author Comment

by:newbieweb
ID: 35020339
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
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 35020909
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
 

Author Comment

by:newbieweb
ID: 35021121
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
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35021437
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
 

Author Closing Comment

by:newbieweb
ID: 35026888
THANKS! THIS WORKS!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now