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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:24:45+00:00 2026-05-19T10:24:45+00:00

I am confused as to how to handle this situation. Usually when an unhandled

  • 0

I am confused as to how to handle this situation.

Usually when an unhandled ASP.Net exception occurs, the server sends back an HTML message of some sort, either the default Asp.Net error handler or a custom error handler. In either case though, HTML is being sent back (and usually it’s a good idea to make the page user friendly).

However, I am having an issue where the unhandled exceptions are occurring in Asp.net MVC controller actions that are expected to return JSON for Ajax calls. When the javascript reads the returned page (which is HTML instead of intended JSON) it crashes due to not being able to convert the response into JSON (right now I’m using ExtJS). I want Json to be returned upon an exception so that the user can be notified that an error has occurred.

The only solution I can think of is to do the following in every action that returns Json:

try { .... }
catch (Exception ex)
{
   return Json(new { success = false, msg = ex.Message });
}

I don’t like that method because it requires me catch all exceptions (which is bad for obvious reasons) and it requires me to pepper every JsonResult action with that same exception handling code (which makes it hard to change later).

Is there a better method to return a more appropriate error result only on action methods that return Json, but still keep regular error pages user friendly for non-Ajax web requests?

  • 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-19T10:24:46+00:00Added an answer on May 19, 2026 at 10:24 am

    Here’s how I solved this problem:

    public class HandleJsonError : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext exceptionContext)
        {
            if (!exceptionContext.HttpContext.Request.IsAjaxRequest() || exceptionContext.Exception == null) return;
    
            exceptionContext.HttpContext.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
            exceptionContext.Result = new JsonResult
                                       {
                                           Data = new
                                                      {
                                                          exceptionContext.Exception.Message,
                                                          exceptionContext.Exception.StackTrace
                                                      }
                                       };
    
            exceptionContext.ExceptionHandled = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am confused on how to handle this below problem. I have a table, and
I am confused how Fortran handle the situation if the index for referencing an
I'm a little bit confused on how handle memory management when dealing with graphics
Pretty confused right now, while i feel i know a bit about vhosts this
Let's say were using custom extensions of the Exception class to handle custom exceptions,
We have a peculiar situation with a particular query. This query joins to a
I've researched other threads and am just confused. Situation: I'm supplying a generic jQuery
So we finally have git set up but I'm really confused how to handle
I'm really confused by this... still. I asked a similar question to this a
Ive always been a bit confused on this, possibly due to my lack of

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.