Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding missing filename on "File Does Not Exist" exception

Posted on 2011-03-02
17
Medium Priority
?
1,658 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 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 2000 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 2000 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
 

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 2000 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 2000 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 2000 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 2000 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

877 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