C# 如何在.GET上忽略对象属性,但在.POST上允许它?

C# 如何在.GET上忽略对象属性,但在.POST上允许它?,c#,asp.net-web-api2,C#,Asp.net Web Api2,我的c#web api项目中有以下对象: public class InfoUtente { public string nome { get; set; } public string cognome { get; set; } public string cellulare { get; set; } public string email { get; set; } public bool termini { get; set; } publ

我的c#web api项目中有以下对象:

public class InfoUtente
{
    public string nome { get; set; }
    public string cognome { get; set; }
    public string cellulare { get; set; }
    public string email { get; set; }
    public bool termini { get; set; }
    public string fidelity { get; set; }

    public InfoUtente() { }
    public InfoUtente(string nome, string cognome, string cellulare, string email, string fidelity)
    {
        this.nome = nome;
        this.cognome = cognome;
        this.cellulare = cellulare;
        this.email = email;
        this.fidelity = fidelity;
    }
}
当对服务器进行
.POST
调用时,将发布
infoute
,在这种情况下,我应该忽略
fidelity
属性并允许
termini

当用户使用
.GET
请求请求数据时,我应该以
保真度
返回整个对象,但不使用
终端

我正在使用默认的序列化程序(JSON.NET),我尝试在
termini
上使用
JsonIgnore
,并在
.GET
调用中使用
termini
返回正确的JSON,但如果我尝试在
termini
属性上使用
JsonIgnore
infoutent
进行
.POST
操作,在.POST中它也将被忽略,并且在任何情况下它的值都将设置为false

因此,我应该能够
。发布带有

nome,cognome,cellulare,email,termini
并且能够
。使用

nome,cognome,cellulare,email,fidelity

我应该制作两个不同的对象吗?一个是带有
终端的
对象,另一个是没有
终端的
对象,另一个是带有
终端的
对象,另一个是带有
终端的
对象。获取
或者我可以只使用一个对象来实现它?

创建两个不同的类确实是最好的解决方案

你可能想了解一下,因为它们也意味着实现你想要做的事情

然后,除了infotunte实体之外,还将有两个类。一个用于获取您感兴趣的特定实例的信息:

public class InfoUtenteDto
{
    public string nome { get; set; }
    public string cognome { get; set; }
    public string cellulare { get; set; }
    public string email { get; set; }
    public string fidelity { get; set; }
}
另一个用于创建新实例:

public class InfoUtenteForCreationDto
{
    public string nome { get; set; }
    public string cognome { get; set; }
    public string cellulare { get; set; }
    public string email { get; set; }
    public bool termini { get; set; }
}

其中一种可能的方法是使用自定义协定解析器类的两个接口。假设除了你的课我们还有

public interface InfoUtentePost
{
    public string nome { get; set; }
    public string cognome { get; set; }
    public string cellulare { get; set; }
    public string email { get; set; }
    public bool termini { get; set; }
}

public interface InfoUtentGet
{
    public string nome { get; set; }
    public string cognome { get; set; }
    public string cellulare { get; set; }
    public string email { get; set; }
    public string fidelity { get; set; }
}
让我们创建一个合同解析器

public class InterfaceContractResolver : DefaultContractResolver
{
    private readonly Type _InterfaceType;
    public InterfaceContractResolver(Type InterfaceType)
    {
        _InterfaceType = InterfaceType;
    }

    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        //IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
        IList<JsonProperty> properties = base.CreateProperties(_InterfaceType, memberSerialization);
        return properties;
    }
}

两个对象可能是最简单的。您应该使用两个单独的模型,因为这允许它们独立地更改,并有各自的需求。从安全性的角度来看,这也是很有用的,因为通过定义一个只表示帖子所需数据的模型,可以防止应用程序受到攻击。
InfoUtentePost product = new InfoUtente();

var settings = new JsonSerializerSettings()
{
    ContractResolver = new InterfaceContractResolver(typeof(InfoUtentePost))
};

string output = JsonConvert.SerializeObject(product, typeof(InfoUtentePost), settings);
 // output = "{\"nome\":null,\"cognome\":null,\"cellulare\":null,\"email\":null,\"termini\":false}"