I have an asp.net application with a HttpModules in it.
It looks at:
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUhe); //UHE on threads
application.Error += new EventHandler(OnError); //regular UHE
When I put in a URL with a bad location I get an System.Web.HttpException
that is captured in my OnError method.
I would like to capture that this a 404 exception and display something
appropriate.
My HttpModule
has:
protected virtual void OnError(object sender, EventArgs args)
{
HttpApplication application = sender as HttpApplication;
Exception ex = application.Server.GetLastError() as Exception;
ProcessTheUHE(ex);
}
What's interesting though is that Application.Response.StatusCode=200, not
404. Is my HttpModule messing something up. Is there a reliable way to
identify that this is a 404?
thanks,
ex.Message
"The file '/Hr-AuthTest/Defaxult.aspx' does not exist."
STACK TRACE
at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
at
System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath
virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean
allowBuildInPrecompile)
at
System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext
context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp,
Boolean allowBuildInPrecompile)
at
System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath
virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
at
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath
virtualPath, Type requiredBaseType, HttpContext context, Boolean
allowCrossApp, Boolean noAssert)
at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context,
String requestType, VirtualPath virtualPath, String physicalPath)
at
System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext
context, String requestType, VirtualPath virtualPath, String physicalPath)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String
requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at
System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&
completedSynchronously)
Walter Wang [MSFT] - 30 Aug 2007 09:12 GMT
Hi Chuck,
At that time in your HTTP module's Error handler, the Response.StatusCode
hasn't been set, which has the default value of 200.
To retrieve the http status code that caused the exception, you can check
for HttpException.GetHttpCode():
HttpException ex = Server.GetLastError() as HttpException;
if (ex != null)
{
int statusCode = ex.GetHttpCode();
}
This should return 404 if the request path isn't found.
Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Chuck P - 04 Sep 2007 15:18 GMT
Thanks Walter; That was it.