Asp.net 如何更改模型绑定名称
我有这样一个输入模型:Asp.net 如何更改模型绑定名称,asp.net,asp.net-core,asp.net-core-2.0,Asp.net,Asp.net Core,Asp.net Core 2.0,我有这样一个输入模型: public class InputModel{ public string SmtpId{get;set;} } public class InputModel{ [JsonProperty("first_name")] public string FirstName{get;set;} } 但我真正想要的是ModelBinder绑定到smtp id,因为这就是数据从SendGrid到达的方式 这可能吗 以下是正在发布的内容: {
public class InputModel{
public string SmtpId{get;set;}
}
public class InputModel{
[JsonProperty("first_name")]
public string FirstName{get;set;}
}
但我真正想要的是ModelBinder绑定到smtp id
,因为这就是数据从SendGrid到达的方式
这可能吗
以下是正在发布的内容:
{
"email":"john.doe@sendgrid.com",
"timestamp": 1337197600,
"smtp-id":"<4FB4041F.6080505@sendgrid.com>",
"sg_event_id":"sendgrid_internal_event_id",
"sg_message_id":"sendgrid_internal_message_id",
"event": "processed"
},
{
“电子邮件”:“约翰。doe@sendgrid.com",
“时间戳”:1337197600,
“smtp id”:“,
“sg\U事件\U id”:“sendgrid\U内部事件\U id”,
“sg\u消息\u id”:“sendgrid\u内部消息\u id”,
“事件”:“已处理”
},
您可以使用JsonProperty
属性装饰您的物业,如下所示:
public class InputModel{
public string SmtpId{get;set;}
}
public class InputModel{
[JsonProperty("first_name")]
public string FirstName{get;set;}
}
它可以用于序列化和反序列化。您可以使用
JsonProperty
属性装饰您的属性,如下所示:
public class InputModel{
public string SmtpId{get;set;}
}
public class InputModel{
[JsonProperty("first_name")]
public string FirstName{get;set;}
}
它将用于序列化和反序列化。正在跟踪此问题: 看来这将在将来的版本中更新。与此同时,这项工作:
[ModelBinder(Name=“smtp id”)]
正在跟踪此问题:
看来这将在将来的版本中更新。与此同时,这项工作:
[ModelBinder(Name=“smtp id”)]
ASP.NET Core 3.0以后版本默认使用System.Text.Json而不是Newtonsoft.Json(也称为Json.NET),因此您应该使用JsonPropertyName
属性:
using System.Text.Json.Serialization;
public class InputModel
{
[JsonPropertyName("smtp-id")]
public string SmtpId { get; set; }
}
如果仍要使用Newtonsoft.Json,则必须:
ASP.NET Core 3.0及以后版本默认使用System.Text.Json而不是Newtonsoft.Json(又称Json.NET),因此您应该使用
JsonPropertyName
属性:
using System.Text.Json.Serialization;
public class InputModel
{
[JsonPropertyName("smtp-id")]
public string SmtpId { get; set; }
}
如果仍要使用Newtonsoft.Json,则必须:
仅供参考:ASP.NET核心利用JSON.NET(Newtonsoft.JSON包)进行JSON序列化/反序列化。因此,对于任何与JSON相关的内容,您都可以查阅该文档。只是ModelBinder似乎没有使用它,因为即使使用该属性,它也没有绑定到模型。您如何绑定
InputModel
?例如,如果您使用的是标准表单编码,那么它将不是JSON,因此此答案将不适用。更新的问题包括sampleFYI:ASP.NET Core利用JSON.NET(Newtonsoft.JSON包)进行JSON序列化/反序列化。因此,对于任何与JSON相关的内容,您都可以查阅该文档。只是ModelBinder似乎没有使用它,因为即使使用该属性,它也没有绑定到模型。您如何绑定InputModel
?例如,如果您使用的是标准表单编码,那么它将不是JSON,所以这个答案将不适用