3

I am trying to create a controller action that accepts a custom model as a parameter.

        [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;
            }
        }

All values in the following model class are properly deserialized, except for OriginUrl and OriginMessageId, which are assigned null in the value parameter properties. I'm thinking that this might have something to do with the underscores in the DataMember(Name = "origin_message_id"). I have tried changing the underscores to hyphens and the same thing happened.

[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; }
}

How can I get the controller to properly deserialize the x-www-form-urlencoded keys that contain underscores or hyphens?

  • do those 2 fields work if you only use letters? – thepaulpage Apr 11 '17 at 14:27
  • @thepaulpage They do work, but as far as I know, they are valid characters and MVC should be deserializing them correctly. I'm not sure where to look, because I'm not sure if the problem exists in something the DataMember attribute specify, or the way the request is being processed and deserialized in MVC. –  Apr 11 '17 at 15:15
  • Are you trying to get json or xml? Are you using postman? This link has a similar issue: http://stackoverflow.com/questions/22492821/webapi-deserializing-and-serializing-alternate-property-names – thepaulpage Apr 11 '17 at 18:03
  • @thepaulpage No, the Content-Type of the request is `application/x-www-form-urlencoded`. –  Apr 11 '17 at 18:05

0 Answers0