6

I have this class

[Serializable]
public class MyObject {
    // properties omitted
}

And this WebAPI controller method:

[HttpPost]
[ResponseType(typeof(string))]
public IHttpActionResult SetMyObject(MyObject o) {
    // process object
}

But it fails to model bind to the MyObject class. The object o in the controller method is completely empty, every property is default (so mostly null).

Turns out this is because of the [Serializable] annotation on MyObject. Removing that makes model binding work again.

Is there a way to keep [Serializable] and fix model binding?

1 Answer 1

9

The answer is in the setting IgnoreSerializableAttribute on the Resolver

((DefaultContractResolver)config.Formatters.JsonFormatter
  .SerializerSettings.ContractResolver)
    .IgnoreSerializableAttribute = true;

Check the:

ASP.NET Web API and [Serializable] class

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

2 Comments

Thanks, I've changed the serialization to Json.NET as suggested in the link.
Great to see that ;) Good luck with Web API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.