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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:40:41+00:00 2026-06-11T12:40:41+00:00

Assuming I sent the following request to my API: POST http://localhost:4940/api/cars HTTP/1.1 User-Agent: Fiddler

  • 0

Assuming I sent the following request to my API:

POST http://localhost:4940/api/cars HTTP/1.1
User-Agent: Fiddler
Host: localhost:4940
Content-Type: application/json
Content-Length: 44

{"Make":"Make1","Year":2010,"Price":10732.2}

And I have the following Car class definition:

public class Car {

    public int Id { get; set; }

    [Required]
    [StringLength(20)]
    public string Make { get; set; }

    [Required]
    [StringLength(20)]
    public string Model { get; set; }

    public int Year { get; set; }

    [Range(0, 500000)]
    public float Price { get; set; }
}

The response I got back is as follows:

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RTpcRHJvcGJveFxCb29rc1xQcm9XZWJBUEkuU2FtcGxlc1xDaGFwdGVyMTNcRGF0YUFubm90YXRpb25WYWxpZGF0aW9uQXR0cmlidXRlc1NhbXBsZVxEYXRhQW5ub3RhdGlvblZhbGlkYXRpb25BdHRyaWJ1dGVzU2FtcGxlXGFwaVxjYXJz?=
X-Powered-By: ASP.NET
Date: Mon, 17 Sep 2012 11:38:58 GMT
Content-Length: 182

{"Message":"The request is invalid.","ModelState":{"car":["Required property 'Model' not found in JSON. Path '', line 1, position 44."],"car.Model":["The Model field is required."]}}

Here is the more readable form of the message body:

{
    "Message": "The request is invalid.",
    "ModelState": {
        "car": [
            "Required property 'Model' not found in JSON. Path '', line 1, position 44."
        ],
        "car.Model":[
            "The Model field is required."
        ]
    }
}

As you can see, I also have one additional error message for car and I am guessing the JsonMediaTypeFormatter performs validation as well and it fails to perform the read action.

Is this the issue here? Is there any way I can suppress the validation at the JsonMediaTypeFormatter level? I don’t want the formatter to perform validation because the validation is also performed by the IBodyModelValidator after the formatter reads the message body.

Edit:

I debugged the source code and it tuns out the JsonMediaTypeFormatter throws an error if a Property is marked with Required and not supplied. the following code is a part from the JsonMediaTypeFormatter:

// Error must always be marked as handled
// Failure to do so can cause the exception to be rethrown at every recursive level and overflow the stack for x64 CLR processes
jsonSerializer.Error += (sender, e) =>
{
    Exception exception = e.ErrorContext.Error;
    formatterLogger.LogError(e.ErrorContext.Path, exception);
    e.ErrorContext.Handled = true;
}

and this triggers the ModelStateFormatterLogger.LogError method which puts the error inside the ModelState:

public void LogError(string errorPath, Exception exception)
{
    if (errorPath == null)
    {
        throw Error.ArgumentNull("errorPath");
    }
    if (exception == null)
    {
        throw Error.ArgumentNull("exception");
    }

    string key = ModelBindingHelper.ConcatenateKeys(_prefix, errorPath);
    _modelState.AddModelError(key, exception);
}

I am still unable to suppress this behavior.

  • 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-11T12:40:42+00:00Added an answer on June 11, 2026 at 12:40 pm

    Sorry I misunderstood your question originally.
    OK here is what you need to do:

    Create a class deriving from the original JsonMediaTypeFormatter and set a custom IRequiredMemberSelector

    public class MyJsonMediaTypeFormatter : JsonMediaTypeFormatter
        {
            public MyJsonMediaTypeFormatter() : base()
            {
                RequiredMemberSelector = new MyRequiredMemberSelector();
            }
        }
    

    In this custom IRequiredMemberSelector just define all properties to be not required.

    public class MyRequiredMemberSelector : IRequiredMemberSelector
    {
        public bool IsRequiredMember(System.Reflection.MemberInfo member)
        {
            return false;
        }
    }
    

    Now replace the default JsonMediaTypeFormatter with the customized one and there you go.

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

Sidebar

Related Questions

Imagine the following: User goes to script ( http://sample.org/test.php ), Script sends an HTTP
Assuming all my Gradle plugin user going to have a MYAPP_HOME sys variable set
Assuming: std::string ToShow,NumStr; The following displays This is 19 ch00: ToShow = This is
assuming I have the following array: views = [ { :user_id => 1, :viewed_at
Assuming i have a post model with created_at, title, and id. How can i
Assuming I'm using some graphic API which allows me to draw bezier curves by
I kick off a network request, assuming no login credentials are required to talk
Question 1) Assuming a user has a timezone setting saved as 'America/New_York' in my
I currently have the following tables for a private messaging forum: alt text http://img159.imageshack.us/img159/45/pmdata.jpg
Assuming of course we're working with HTML Forms and GET/POST data, backend language agnostic.

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.