绑定[Serializable]类的ASP.Net MVC模型

Chr*_*aks 3 c# asp.net-mvc model-binding asp.net-web-api

我有这堂课

[Serializable]
public class MyObject {
    // properties omitted
}
Run Code Online (Sandbox Code Playgroud)

以及此WebAPI控制器方法:

[HttpPost]
[ResponseType(typeof(string))]
public IHttpActionResult SetMyObject(MyObject o) {
    // process object
}
Run Code Online (Sandbox Code Playgroud)

但是它无法建模绑定到MyObject类。控制器方法中的对象o完全为空,每个属性均为默认值(因此通常为null)。

事实证明,这是由于MyObject上的[Serializable]注释所致。删除后,模型绑定将再次起作用。

有没有办法保持[可序列化]并修复模型绑定?

Rad*_*ler 5

答案在解析器IgnoreSerializableAttribute上的设置中

((DefaultContractResolver)config.Formatters.JsonFormatter
  .SerializerSettings.ContractResolver)
    .IgnoreSerializableAttribute = true;
Run Code Online (Sandbox Code Playgroud)

检查:

ASP.NET Web API和[Serializable]类