I am trying to understand what error occurred that sent me to the Global ASAX OnError handler.
using System;
using System.Web;
namespace GLSS.Components.HttpModules
{
public class ExceptionModule : System.Web.IHttpModule
{
private void OnError(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
//get the last error
Exception ex = context.Server.GetLastError();
if(ex.InnerException.GetType().ToString() == "CSLA.DataPortalException")
ex = ex.InnerException;
Here is my Exception converted to a String
HttpContext.Current.Server.GetLastError().Message
"File does not exist."
HttpContext.Current.Server.GetLastError().StackTrace
" 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)"
How do I determine what line of code is causing this error? I tried to set my Debug options to break when the error occurs, but it didn’t , I still end up in the ONERROR global handler.
One thing is that I see that the code assumes that there will be an Inner Exception, and this appears to be NULL and is causing a second error within the handler.
I assume that the error is occuring somewhere in compiled code. I checked the Web.Config, and the only paths mentioned there is a Log Path, and that seems valid and logging appears to be working.
Update
I found some additional information here:
How to solve exception "File does not exist"?
When I check this in the Immediate window:
? HttpContext.Current.Request.Url.ToString()
"http://localhost:2322/favicon.ico"
However, what puzzles me is that I search my entire solution looking for favicon.ico using “Find In Files” and I see no reference.
Why am I getting an error that the icon file is not found when I see no reference to it? I’m guess some assembly is using it? But why is it looking for it in the web root?
The request for favicon.ico is being blindly made by most modern browsers and they are expecting a 404 (File Not Found) if there is no favicon (this is proper behaviour). Below you can find a quote from HTML5 working draft regarding Link type “icon”
:
The reason why you see the exception is that the web development server or IIS configured to use Managed/Integrated Pipeline Mode puts all requests through Global.asax (including errors).
You can try to prevent the browsers from making the requests by creating following dummy link to favicon:
You also might try one of following:
Add following line at beginning of
RegisterRoutesmethod:routes.IgnoreRoute("favicon.ico");or even more extended version:
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});HttpException.GetHttpCode()for 404 and((System.Web.HttpApplication)Sender).Context.Request.Urlfor /favicon.ico.