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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:09:29+00:00 2026-06-08T09:09:29+00:00

In the default implementation of the JsonValueProviderFactory, JavaScriptSerializer.DeserializeObject() method is used. This method throws

  • 0

In the default implementation of the JsonValueProviderFactory, JavaScriptSerializer.DeserializeObject() method is used. This method throws an exception if the json string is ill-formed. The server then throws a 500 yellow page to the browser. I want to suppress the exception and show a custom error message through my controller action.

This is what I tried. I removed the default JsonValueProviderFactory and introduced a custom JsonValueProviderFactory with essentially the same code. The only difference is in the GetDeserializedObject method where I have introduced a try-catch block.

private static object GetDeserializedObject(ControllerContext controllerContext)
    {
        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
        {
            // not JSON request
            return null;
        }

        StreamReader reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
        string bodyText = reader.ReadToEnd();
        if (String.IsNullOrEmpty(bodyText))
        {
            // no JSON data
            return null;
        }

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        object jsonData = null;
        try
        {
            jsonData = serializer.DeserializeObject(bodyText);
        }
        catch (Exception ex) {
            ex.Data["jsonString"] = bodyText;
            throw;
        }
        return jsonData;
    }

Instead of simply throwing the exception, I want to pass the exception error message to the modelbinder and eventually set it in the modelstate dictionary. Then I can read the modelstate dictionary in controller action and customize the error message sent to browser. Can this be done ?
If it can’t be done, is there an alternative way to achieve the same goal ?

  • 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-08T09:09:31+00:00Added an answer on June 8, 2026 at 9:09 am

    I finally found the right way of doing it. I am documenting it for future reference by others.

    controllerContext.Controller.ViewData.ModelState.AddModelError("improper json", ex)
    

    This is exactly how the modelstate in modelbindingContext of modelbinder is set. I copying the relevant snippent of code from MVC3 sources. The method is present in ControllerActionInvoker.cs.

     protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) {
            // collect all of the necessary binding properties
            Type parameterType = parameterDescriptor.ParameterType;
            IModelBinder binder = GetModelBinder(parameterDescriptor);
            IValueProvider valueProvider = controllerContext.Controller.ValueProvider;
            string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
            Predicate<string> propertyFilter = GetPropertyFilter(parameterDescriptor);
    
            // finally, call into the binder
            ModelBindingContext bindingContext = new ModelBindingContext() {
                FallbackToEmptyPrefix = (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType),
                ModelName = parameterName,
                ModelState = controllerContext.Controller.ViewData.ModelState,
                PropertyFilter = propertyFilter,
                ValueProvider = valueProvider
            };
    
            object result = binder.BindModel(controllerContext, bindingContext);
            return result ?? parameterDescriptor.DefaultValue;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to documentation Default implementation does nothing. But... I throw exception from drawRect method
MSDN says : The default implementation of the GetHashCode method does not guarantee unique
If I did this // Default implementation of UNIX time of the current UTC
This might be trivial, but I don't understand why the default implementation of Selection
I have implemented Spring Security in my application. I have used default implementation, i.e.,
Is it better to put a default implementation of a method in a superclass,
I want to redefine an (default) implementation in a given windsor-container. Is that what
If I understand correctly, in .NET the default implementation of Object.GetHashCode() returns a value
I created an UIViewController subclass, and figured out that the default implementation of -loadView
I have to confess that I hate membership provider. The default implementation is not

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.