Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

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
Avatar of curiouswebster
curiouswebster
Flag of United States of America image

ASKER

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
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;
SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your code works fine, but the value for fex is still null. So the filename remains null. This must not be a FileNotFoundException.
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();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Undefined. See attached...
Exception.PNG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS! THIS WORKS!!!