Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6203007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:50:21+00:00 2026-05-24T04:50:21+00:00

I have code in the global.asax file’s Application_Error event which executes when an error

  • 0

I have code in the global.asax file’s Application_Error event which executes when an error occurs and emails details of the error to myself.

void Application_Error(object sender, EventArgs e)
{
    var error = Server.GetLastError();

    if (error.Message != "Not Found")
    {
        // Send email here...
    }

}

This works fine when I’m running it in Visual Studio, however when I publish to our live server the Application_Error event does not fire.

After some testing I can get the Application_Error firing when I set customErrors="Off", however setting it back to customErrors="On" stops the event from firing again.

Can anyone suggest why Application_Error would not be firing when customErrors are enabled in the web.config?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:50:22+00:00Added an answer on May 24, 2026 at 4:50 am

    UPDATE
    Since this answer does provide a solution, I will not edit it, but I have found a much cleaner way of solving this problem. See my other answer for details…

    Original Answer:
    I figured out why the Application_Error() method is not being invoked…

    Global.asax.cs

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute()); // this line is the culprit
        }
    ...
    }
    

    By default (when a new project is generated), an MVC application has some logic in the Global.asax.cs file. This logic is used for mapping routes and registering filters. By default, it only registers one filter: a HandleErrorAttribute filter. When customErrors are on (or through remote requests when it is set to RemoteOnly), the HandleErrorAttribute tells MVC to look for an Error view and it never calls the Application_Error() method. I couldn’t find documentation of this but it is explained in this answer on programmers.stackexchange.com.

    To get the ApplicationError() method called for every unhandled exception, simple remove the line which registers the HandleErrorAttribute filter.

    Now the problem is: How to configure the customErrors to get what you want…

    The customErrors section defaults to redirectMode="ResponseRedirect". You can specify the defaultRedirect attribute to be a MVC route too. I created an ErrorController which was very simple and changed my web.config to look like this…

    web.config

    <customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/Error">
      <error statusCode="404" redirect="~/Error/PageNotFound" />
    </customErrors>
    

    The problem with this solution is that it does a 302 redirect to your error URLs and then those pages respond with a 200 status code. This leads to Google indexing the error pages which is bad. It also isn’t very conformant to the HTTP spec. What I wanted to do was not redirect, and overrite the original response with my custom error views.

    I tried to change redirectMode="ResponseRewrite". Unfortunately, this option does not support MVC routes, only static HTML pages or ASPX. I tried to use an static HTML page at first but the response code was still 200 but, at least it didn’t redirect. I then got an idea from this answer…

    I decided to give up on MVC for error handling. I created an Error.aspx and a PageNotFound.aspx. These pages were very simple but they had one piece of magic…

    <script type="text/C#" runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Response.StatusCode = (int) System.Net.HttpStatusCode.InternalServerError;
        }
    </script>
    

    This block tells the page to be served with the correct status code. Of course, on the PageNotFound.aspx page, I used HttpStatusCode.NotFound instead. I changed my web.config to look like this…

    <customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/Error.aspx">
      <error statusCode="404" redirect="~/PageNotFound.aspx" />
    </customErrors>
    

    It all worked perfectly!

    Summary:

    • Remove the line: filters.Add(new HandleErrorAttribute());
    • Use Application_Error() method to log exceptions
    • Use customErrors with a ResponseRewrite, pointing at ASPX pages
    • Make the ASPX pages responsible for their own response status codes

    There are a couple downsides I have noticed with this solution.

    • The ASPX pages can’t share any markup with Razor templates, I had to rewrite our website’s standard header and footer markup for a consistent look and feel.
    • The *.aspx pages can be accessed directly by hitting their URLs

    There are work-arounds for these problems but I wasn’t concerned enough by them to do any extra work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like the following, which works fine for displaying the
I have defined a series of Routes in Global.asax.cs: public static void RegisterRoutes(RouteCollection routes)
I have code that references a web service, and I'd like the address of
I have code to create another row (div with inputs) on a button click.
I have code similar to this filtering entries in an Array of Objects: var
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code similar to the following in many places: var dbParams = db.ReadParams(memberID,
I have code like this to move the player in my game left, right,
I have code in an Update Panel and even though on a button click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.