Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api Dash in variable name model binder

I would like to implement Mailgun's webhooks into my .Net Web API application, but some of the parameters they POST have dash'es in them. How do I get around that?

Example of what they post:

client-type=browser&city=San+Francisco&domain=telzio.com&device-type=desktop&my_var_1=Mailgun+Variable+%231&country=US&region=CA&client-name=Chrome&user-agent=Mozilla%2F5.0+%28X11%3B+Linux+x86_64%29+AppleWebKit%2F537.31+%28KHTML%2C+like+Gecko%29+Chrome%2F26.0.1410.43+Safari%2F537.31&client-os=Linux&my-var-2=awesome&ip=50.56.129.169&recipient=alice%40example.com&event=opened&timestamp=1405017113&token=6khi46bvupa1358v0b3iy29kwumpbajb3ioz4illb6v9bbqkp6&signature=88f46b9ba63ff475bbb3ab193696cf45bf2f25e7e62b44f1e492ff4e085730dd

My model:

public class MailgunModel
{
    public string City { get; set; }
    public string Domain { get; set; }
    public string Country { get; set; }
    public string Region { get; set; }
    public string Ip { get; set; }
    public string Recipient { get; set; }
    public string Event { get; set; }
    public long Timestamp { get; set; }
    public string Token { get; set; }
    public string Signature { get; set; }

    public string ClientType get; set; }
    public string DeviceType { get; set; }
    public string ClientName { get; set; }
    public string UserAgent { get; set; }
    public string ClientOs { get; set; }
}
like image 227
Peter Schrøder Avatar asked Jul 10 '14 20:07

Peter Schrøder


People also ask

How to use model binder with MVC and web API?

The model binder class with both MVC and Web API implementations. We use a model binder as a parameter of the MVC and Web API Action Result. The parameter environmentInfo will contain all the information we want about the URL and logged user and other things.

How does web API bind parameters to a controller?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters:

How do I create a model-binder provider?

A model-binder provider is simply a factory class that creates a model binder. You can create a provider by deriving from the ModelBinderProvider class. However, if your model binder handles a single type, it's easier to use the built-in SimpleModelBinderProvider, which is designed for this purpose. The following code shows how to do this.

How do I add aliasmodelbinder parameter to a WebAPI call?

In order to ensure that this runs as part of a WebAPI call you must also add config.BindParameter (typeof (TestModelType), new AliasModelBinder ()); in the Regiser portion of your WebApiConfig. If you are using this method, you also must remove [FromBody] from your method signature.


Video Answer


1 Answers

One of the easiest ways is to receive a FormDataCollection and access the variables you need. It's lame because you have to map manually each property but it works for simple scenarios.

public IHttpActionResult AppointmentMessage(FormDataCollection data)
{
    if (data != null)
    {
        var msg = new MailGunMessage();
        msg.From = data["from"];
        msg.To = data["to"];
        msg.Subject = data["subject"];
        msg.BodyHtml = data["body-html"];
        msg.BodyPlain = data["body-pain"];
        // ... and so on
    }
    return this.Ok();
 }

An other options is to use a custom model binder on your model like described here: https://stackoverflow.com/a/4316327/1720615

like image 76
Chris Avatar answered Oct 28 '22 19:10

Chris