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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:41:49+00:00 2026-06-07T10:41:49+00:00

I was trying to return an error to the call to the controller as

  • 0

I was trying to return an error to the call to the controller as advised in
This link so that client can take appropriate action.
The controller is called by javascript via jquery AJAX. I am getting the Json object back only if I don’t set the status to error.
Here is the sample code

if (response.errors.Length > 0)
   Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(response);

I get the Json if I don’t set the statuscode.
If I set the status code I get the status code back but not the Json error object.

Update
I want to send an Error object as JSON so that it can be handled error callback of ajax.

  • 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-07T10:41:52+00:00Added an answer on June 7, 2026 at 10:41 am

    I found the solution here

    I had to create a action filter to override the default behaviour of MVC

    Here is my exception class

    class ValidationException : ApplicationException
    {
        public JsonResult exceptionDetails;
        public ValidationException(JsonResult exceptionDetails)
        {
            this.exceptionDetails = exceptionDetails;
        }
        public ValidationException(string message) : base(message) { }
        public ValidationException(string message, Exception inner) : base(message, inner) { }
        protected ValidationException(
        System.Runtime.Serialization.SerializationInfo info,
        System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
    

    Note that I have constructor which initializes my JSON. Here is the action filter

    public class HandleUIExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        public virtual void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            if (filterContext.Exception != null)
            {
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
                filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                filterContext.Result = ((ValidationException)filterContext.Exception).myJsonError;
            }
        }
    

    Now that I have the action filter, I will decorate my controller with the filter attribute

    [HandleUIException]
    public JsonResult UpdateName(string objectToUpdate)
    {
       var response = myClient.ValidateObject(objectToUpdate);
       if (response.errors.Length > 0)
         throw new ValidationException(Json(response));
    }
    

    When the error is thrown the action filter which implements IExceptionFilter get called and I get back the Json on the client on error callback.

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

Sidebar

Related Questions

I am trying to secure a controller action that is being called with the
Im trying to return the value that a $ajax call returns, from a function
I'm trying to call a server-side action in a controller from jQuery: $.ajax({ url:'http://localhost:88/admin/business/11/GetChildBusinessTypes',
i am noticing a error when im trying to return json from a fetchAll.
Im trying to return a SimpleQuery list that queries a single table and uses
I'm trying to return a JSON response from a Django view call via an
I'm having an odd problem. My controller is trying to call uri segments and
I am trying to call a controller via ajax without to much luck. I
I've been trying to figure this out but can't find any answers to my
I'm trying to create a class called HighscoresController which subclasses NSObject. When I call

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.