FromForm属性不使用Name属性中的下划线或连字符反序列化DataMember属性

时间:2017-04-10 19:50:22

标签: c# deserialization asp.net-core-mvc

我正在尝试创建一个接受自定义模型作为参数的控制器操作。

        [HttpPost]
        public void SendEvent([FromForm]WebHookRequest value)
        {
            SiteNotification notification = Mapper.Map<SiteNotification>(value);

            if (notification != null)
            {
                Task.Run(() => { _siteNotifier.NotifySite(notification); });
                Response.StatusCode = 200;
            }
            else
            {
                // Set the status code that mailgun expects on failure
                Response.StatusCode = 406;
            }
        }

以下模型类中的所有值都已正确反序列化,但OriginUrl和OriginMessageId除外,它们在value参数属性中指定为null。我认为这可能与DataMember(Name = "origin_message_id")中的下划线有关。我尝试将下划线更改为连字符,同样的事情发生了。

[DataContract]
public class WebHookRequest
{
    [DataMember(Name = "event")]
    public string Event { get; set; }

    [DataMember(Name = "recipient")]
    public string Recipient { get; set; }

    [DataMember(Name = "domain")]
    public string Domain { get; set; }

    [DataMember(Name = "origin_url")]
    public string OriginUrl { get; set; }

    [DataMember(Name = "origin_message_id")]
    public string OriginMessageId { get; set; }

    [DataMember(Name = "timestamp")]
    public string TimeStamp { get; set; }

    [DataMember(Name = "token")]
    public string Token { get; set; }

    [DataMember(Name = "signature")]
    public string Signature { get; set; }
}
  

如何让控制器正确反序列化包含下划线或连字符的 x-www-form-urlencoded 键?

0 个答案:

没有答案