0

I have wasted a day already and still do not quite understand how can I process ALL HTTP exceptions with my ASP.NET logging method and Server.Transfer to my custom error page then?!

What I have tried:

  1. <customErrors redirectMode="ResponseRewrite"><error statusCode="404" redirect="Error.aspx?404" />. Works only for .aspx files. Not works for non-aspx 404th. Can't Server.Transfer to dynamically generated URL. Strangelly not works with asHx handler, works with asPx pages.

  2. <httpErrors errorMode="Custom" existingResponse="Replace" ><error statusCode="404" path="Error.aspx?404" responseMode="ExecuteURL" />. Didn't understand how it works. Shows white screen instead of my file when responseMode="ExecuteURL" is set. responseMode="Redirect" works okay, but I do not like the redirect. Can't Server.Transfer to dynamically generated URL.

  3. Custom IHttpModule with preCondition="" and context.Error += new EventHandler(OnError);. For 500th it works pretty well, but unfortunately, for 404th works only for aspx files. www.website.com/1234.aspx - works okay. www.website.com/2345 - not works.

So... how is it possible to handle all HTTP exceptions?

1 Answer 1

1

options1&2 are only going to apply to requests handled by asp.net. You need to change the wildcard script mapping to asp.net. http://www.iis.net/learn/application-frameworks/building-and-running-aspnet-applications/wildcard-script-mapping-and-iis-integrated-pipeline

for #3 you need to use the runAllManagedModulesForAllRequests on the modules section of the web.config. explained at the end of the same module

UPDATE:

This worked for me. I hope it's closer to what you want.

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
    </system.web>
    <system.webServer>
        <handlers>
            <remove name="StaticFile"/>
            <add name="StaticFile"  type="System.Web.StaticFileHandler" path="*" verb="*" resourceType="Unspecified" requireAccess="Read" />
        </handlers>
        <modules runAllManagedModulesForAllRequests="false"/>
    </system.webServer>
</configuration>

Handle error with Global.asax

public class Global : System.Web.HttpApplication
{
    protected void Application_Error(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext currentContext = application.Context;
        Exception currentException = application.Server.GetLastError();
        Debug.WriteLine("Application_Error " + currentContext.Request.PhysicalPath);
        application.Server.Transfer("~/CustomErrorTransfer.aspx");
    }
}

Or in an HttpModule

public class ErrorHandlerModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += ContextOnError;
    }

    private static void ContextOnError(object sender, EventArgs eventArgs)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext currentContext = application.Context;
        Debug.WriteLine("ErrorHandlerModule " + currentContext.Request.PhysicalPath);
        Exception currentException = application.Server.GetLastError();
        application.Server.ClearError();
        application.Server.Transfer("~/CustomErrorTransfer.aspx");
    }
}

with this added to the web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="false">
        <add name="Errors" type="MyRootNamespace.ErrorHandlerModule"/>
    </modules>
</system.webServer>

CustomErrorTransfer.aspx used by other pages

<%@ Page Language="C#" %>
<!DOCTYPE html>
<head runat="server"><title></title></head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Custom Error Page</h1>
        Current Path: <%= Request.CurrentExecutionFilePath  %><br/>
        Original Path: <%=  Request.PhysicalPath %><br/>
    </div>
    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Sir, before I asked my question, I tried both preCondition="" and runAllManagedModulesForAllRequests="true" in #3. It does not work for non-aspx 404th. It hits BeginRequest event, it hits EndRequest event, but it does not hit Error event. For aspx 404th and 500th it works great. Is there a workaround?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.