ASP.Net MVC Model binding a [Serializable] class

…衆ロ難τιáo~ 提交于 2019-12-22 10:59:16

问题


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:


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



来源:https://stackoverflow.com/questions/28557063/asp-net-mvc-model-binding-a-serializable-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!