绑定[Serializable]类的ASP.Net MVC模型 [英] ASP.Net MVC Model binding a [Serializable] class

查看:79
本文介绍了绑定[Serializable]类的ASP.Net MVC模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这堂课

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

此WebAPI控制器方法:

And this WebAPI controller method:

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

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

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).

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

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?

推荐答案

答案是在 Resolver

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

检查:

ASP.NET Web API和[可序列化]类

这篇关于绑定[Serializable]类的ASP.Net MVC模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆