简体   繁体   中英

Binding JSON with property of name “event” in asp.net MVC controller

How can I bind the property named "event" below with the class I have listed through an ASP.NET MVC controller action?

Class

public class SendGridWebhookRequest
    {
        public string email { get; set; }
        public int timestamp { get; set; }
        public string smtp_id { get; set; }
    }

Request

{
  "status":"5.0.0",
  "sg_event_id":"sendgrid_internal_event_id",
  "sg_message_id":"sendgrid_internal_message_id",
  "event":"bounce",
  "email":"email@example.com",
  "timestamp":1249948800,
  "smtp-id":"<original-smtp-id@domain.com>",
  "unique_arg_key":"unique_arg_value",
  "category":["category1", "category2"],
  "reason":"500 No Such User",
  "type":"bounce"
}

You can give a custom name and specify the name of the property in json with an attribute:

public class SendGridWebhookRequest
{
    public string email { get; set; }
    public int timestamp { get; set; }
    public string smtp_id { get; set; }
    [JsonProperty("event")]
    public string MyEvent {get;set;}
}
public string @event {get;set;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM