Today I got a problem from my colleague, that he is getting FileNot Found exception in Application Error event in Global.asax, which is used to log application errors. But the application was working fine and good. We thought it may be because of missing images that was referring in Stylesheets. But all the image references are valid and we couldn’t find any issue. We couldn’t find anything suspicious in stack trace.
Stack Trace from the exception
at System.Web.StaticFileHandler.GetFileInfo(String virtualPathWithPathInfo, String physicalPath, HttpResponse response)
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
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)
After doing some search we got an option where we can find the file causing the exception. We created a string variable and put a break point over there and debugged the code. It will display the filename causing the exception. In our case it was a HTML file which was used in a Javascript to set an IFRAME source, unfortunately the developer who copied the script missed the HTML and it was causing the exception.
Here is the code.
void Application_Error(object sender, EventArgs e)
{
Exception err = Server.GetLastError();
//Insert a break point and run in debug mode.
string fileName = Context.Request.FilePath;
Application["UnhandledException"] = err;
}
Nice Tip !
void Application_Error(object sender, EventArgs e) { Exception err = Server.GetLastError(); //Insert a break point and run in debug mode. string fileName = Context.Request.FilePath; Application["UnhandledException"] = err; }
Above code will never show the detail of that exception if you dont know then you should not write anything here .. but atleast you should not make fool to people