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 6747797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:29:14+00:00 2026-05-26T12:29:14+00:00

What is the accepted way to handling errors and trapping exception in asp.net mvc

  • 0

What is the accepted way to handling errors and trapping exception in asp.net mvc structure?
The general consensus is to have exceptions bubble up.
So what layer (View or Controller) would you handle the exceptions (catch them/ display user friendly text, etc). My hunch is that it is done in the controller?
EDITED:
I would like to avoid repeating the same error handling code in every controller action..hence I am looking for a concise example of how to implement error handling without repeating the same code.

  • 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-26T12:29:14+00:00Added an answer on May 26, 2026 at 12:29 pm

    Handling exceptions in the controller could lead to a lot of repetitive code. A better approach is to handle them in an action filter that extends HandleErrorAttribute. There you could log exceptions and then redirect to a page that displays a nice message indicating the user that something went wrong.

    However, there are some cases where you need to handle exceptions in your controller methods, for example, when you can recover from an exception and show the user an appropriate message, for example an exception that was throw by your business layer indicating that the values you provided are not valid. In that case, you should catch the specific exception and show the user the same view along with an appropriate message.

    EDIT:

    public class CustomErrorHandlerAttribute : HandleErrorAttribute
    {
         public override void OnException(ExceptionContext filterContext)
         {
             var logger = log4net.LogManager.GetLogger("SomeLoggerHere");
    
             logger.Error("An unhandled error occurred", filterContext.Exception);
    
             if (filterContext.HttpContext.Request.IsAjaxRequest())
             {
                 filterContext.HttpContext.Response.Clear();
                 filterContext.HttpContext.Response.Status = "500 Internal Server Error";
                 filterContext.Result = new JsonResult { Data = new { ErrorMessage = filterContext.Exception.Message } };
                 filterContext.ExceptionHandled = true;                
             }
             else
             {
                 base.OnException(filterContext);
             }
    
        }
    

    }

    EDIT 2:
    Then you use the attribute like this:

    [CustomErrorHandler]
    public class AnyController : Controller
    {
    ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coming from a .net background and want to know the accepted way of
A generally accepted way to pass all data to a view is to have
Is there an accepted way of performing (pinch) zoom on a view that is
There doesn't seem to be an accepted way of sending down a header parameter
Is there an easy (or generally accepted) way to load up a binary column
In Java, what's generally the most accepted way to organize a class in terms
This is related to the accepted answer for What’s your #1 way to be
Is there a way to convert from System.Windows.Forms.HtmlElement to mshtml.IHTMLElemenet3? --edit after answer accepted
What is the widely-accepted way to store interface choices, such as 'Do not show
Bit of a dumb question, but I'm wondering what the accepted way of passing

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.