Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何更改模型绑定名称_Asp.net_Asp.net Core_Asp.net Core 2.0 - Fatal编程技术网

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,则必须:

  • 安装Microsoft.AspNetCore.Mvc.NewtonsoftJson包
  • 将Newtonsoft.Json设置为Startup.cs中的默认输入/输出格式化程序

  • 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,则必须:

  • 安装Microsoft.AspNetCore.Mvc.NewtonsoftJson包
  • 将Newtonsoft.Json设置为Startup.cs中的默认输入/输出格式化程序

  • 仅供参考:ASP.NET核心利用JSON.NET(Newtonsoft.JSON包)进行JSON序列化/反序列化。因此,对于任何与JSON相关的内容,您都可以查阅该文档。只是ModelBinder似乎没有使用它,因为即使使用该属性,它也没有绑定到模型。您如何绑定
    InputModel
    ?例如,如果您使用的是标准表单编码,那么它将不是JSON,因此此答案将不适用。更新的问题包括sampleFYI:ASP.NET Core利用JSON.NET(Newtonsoft.JSON包)进行JSON序列化/反序列化。因此,对于任何与JSON相关的内容,您都可以查阅该文档。只是ModelBinder似乎没有使用它,因为即使使用该属性,它也没有绑定到模型。您如何绑定
    InputModel
    ?例如,如果您使用的是标准表单编码,那么它将不是JSON,所以这个答案将不适用