3

I have a small problem with the WebApi.

Problem: If I want to post a model using JSON, I can add as many members I want, as long as the members defined in model are present.

Question: How can I trigger an exception, if an undefined member is present in my Json object. Is this achievable without a custom JsonConverter? What I'm looking for is a generic solution, not a convertion for every different model.

Example:

Model:

public class Person
{
    [Required]
    public string Name { get; set; }
}

Api Controller:

public class PersonController : ApiController
{
    public HttpResponseMessage Post(Person person)
    {
        if (person != null)
        {
            if (ModelState.IsValid)
            {
                //do some stuff
                return new HttpResponseMessage(HttpStatusCode.OK);
            }
        }
        return new HttpResponseMessage(HttpStatusCode.BadRequest);

    }
}

Json posts (body) {"Name":"Joe"} --> valid {"Name":"Joe","InvalidMember","test","Name","John"} --> also valid. In this case I want to trigger an Exception. Because if you look at it, it doesn't match my modeldefinition exactly.

3
  • Indeed, it's not really a problem, but I want to force my clients to use the API as described and not fill it with unuseful information. It's just consuming bandwidth. If the API isn't making a problem of it, the client keeps posting extra data, probably without even knowing it. Commented Apr 3, 2013 at 14:40
  • Posting extra data would still consume bandwidth. The data is already transferred to the server by the time you are checking its structure. Not sure if you are trying to prevent extra data to be posted. In that case you'd have to work something out on the client side. Commented Apr 3, 2013 at 15:17
  • Yes I know. All I want is making the client aware is posting extra data, that's all. "Hey you're posting data I don't need. Clean up your post and try again". If you ignore the extra data, the client will keep posting it, which will result in unnecessary network traffic. Commented Apr 4, 2013 at 7:18

1 Answer 1

3

One thing you could try is playing around with this setting:

config.Formatters.JsonFormatter.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;

It should give you an invalid model state when there are extra properties that aren't recognized in the JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

The naming of this seems counter-intuitive, I would have thought it's for under-posting scenarios (member is missing from JSON), not over-posting (member is missing from object definition). But the documentation on it is quite clear: "Get or set how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization." - james.newtonking.com/projects/json/help/index.html?topic=html/…
Thanks. This solves my problem. I knew this setting exists, but I was on the assumption that it only handles under-posting. Lesson learned: never assume anything ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.