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

  • Home
  • SEARCH
  • 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 8457299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:46:34+00:00 2026-06-10T12:46:34+00:00

I have read articles on exception handling in ASP.NET MVC. I want to make

  • 0

I have read articles on exception handling in ASP.NET MVC. I want to make sure I am doing right by presenting it briefly. Could anyone please comment.

  1. Catch the exceptions in controller actions, if necessary.

    [HttpPost]
    public ActionResult Insert()
    {
        try
        {
    
        }
        catch
        {
            //ModelState.Error -> display error msg to the user.
        }
    }
    
  2. Override the “OnException” method of controller in basecontroller and “log” the exceptions raised in step 1 and other MVC exceptions

  3. Logged the global exceptions in application_onerror.

  • 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-06-10T12:46:36+00:00Added an answer on June 10, 2026 at 12:46 pm

    I would definitely recommend ELMaH instead of writing this code yourself, and also over Log4Net for your MVC apps. I personally avoid any exception handling, unless I have a specific functional response to it. In this way, I don’t “eat” any of the errors that an application-wide tool such as ELMaH will handle gracefully for me.

    ELMaH also has nice built-in web reporting, and there are third-party tools specifically for ELMaH that can give you statistics, e.g. the most frequent errors.

    You might start with a custom error redirect…

    <customErrors defaultRedirect="~/site/error" mode="RemoteOnly">
      <error statusCode="404" redirect="~/site/notfound" />
    </customErrors>
    

    …to a controller that is aware you are using ELMaH…

    public virtual ActionResult Error() {
        System.Collections.IList errorList = new System.Collections.ArrayList();
        ErrorLog.GetDefault(System.Web.HttpContext.Current).GetErrors(0, 1, errorList);
        ErrorLogEntry entry = null;
        if (errorList.Count > 0) {
            entry = errorList[0] as Elmah.ErrorLogEntry;
        }
        return View(entry);
    }
    

    …backed by a view that helps the visitor get the specific error ID to you:

    @model Elmah.ErrorLogEntry
    
    @if (Context.User.Identity.IsAuthenticated) {
        <p>Since you are signed in, we've noted your contact information,
        and may follow up regarding this to help improve our product.</p>
    } else {
        <p>Since you aren't signed in, we won't contact you regarding this.</p> 
    }
    <p>Error ID: @Model.Id</p>
    

    I also notice this is an HttpPost in this example. If you are doing AJAX, then you’ll want to handle errors for those in a unique way. Pick a standard response you can send to browsers that all of your AJAX code handles gracefully. Perhaps by displaying the ELMaH error ID in a javascript alert (as a simple example).

    I also handle a few special types of AJAX errors via Global.asax:

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 302 &&
            Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
    

    HandleErrorAttribute is a nice feature, but it is well-known that there is extra work to use it in conjunction with ELMaH. How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

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

Sidebar

Related Questions

I am new to ASP.net MVC architecture. I have read in some articles that
I'm working on the exception handling layer for my application. I have read few
I have a scenario similar to this: Asp.NET MVC 4 website using nHibernate Session
I have read few articles about table partioning but still I am bit confused
I have read several articles and questions on concept of foreign key vs independent
I have read various articles about passing variables around using RMI. Some of them
I have read multiple articles about why singletons are bad. I know it has
I have read some articles on POCO in the enttity framework but still don't
I am trying understand ViewModels deeper and I have read many articles and blogs
I have read through several articles which are alternatives to using setpixel/getpixel but I

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.