4

I have the following model

public class Dog
{
    public string NickName { get; set; }
    public int Color { get; set; }
}

and I have the following api controller method which is exposed through an API

public class DogController : ApiController
{
  // GET /v1/dogs
  public IEnumerable<string> Get([FromUri] Dog dog)
  { ...}

Now, I would like to issue the GET request as follows:

GET http://localhost:90000/v1/dogs?nick_name=Fido&color=1

Question: How do I bind the query string parameter nick_name to property NickName in the dog class? I know I can call the API without the underscore (i.e. nickname) or change NickName to Nick_Name and get the value, but I need the names to remain like that for convention.

Edit This question is not a duplicate because it is about ASP.NET WebApi not ASP.NET MVC 2

yodadev
  • 100
  • 2
  • 11
  • Your question is about Binding a model property to an alias, correct? [This answer](http://stackoverflow.com/a/4316327/1454538) on that post does exactly that. – matt. Oct 21 '15 at 00:12
  • 1
    There is a difference between implementing System.Web.Mvc.IModelBinder and System.Web.Http.ModelBinding.IModelBinder. BindModel for MVC is [object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)] and for WebApi is [bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)]. The examples I see in your possible duplicate only cover MVC not WebApi – yodadev Oct 21 '15 at 00:32

1 Answers1

4

Implementing the IModelBinder,

public class DogModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(Dog))
        {
            return false;
        }

        var model = (Dog)bindingContext.Model ?? new Dog();


        var hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);

        var searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";

        model.NickName = GetValue(bindingContext, searchPrefix, "nick_name");

        int colorId = 0;
        if (int.TryParse(GetValue(bindingContext, searchPrefix, "colour"), out colorId))
        {
            model.Color = colorId; // <1>
        }

        bindingContext.Model = model;

        return true;
    }

    private string GetValue(ModelBindingContext context, string prefix, string key)
    {
        var result = context.ValueProvider.GetValue(prefix + key); // <4>
        return result == null ? null : result.AttemptedValue;
    }
}

And Create ModelBinderProvider,

public class DogModelBinderProvider : ModelBinderProvider
{
    private CollectionModelBinderProvider originalProvider = null;

    public DogModelBinderProvider(CollectionModelBinderProvider originalProvider)
    {
        this.originalProvider = originalProvider;
    }

    public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
    {
        // get the default implementation of provider for handling collections
        IModelBinder originalBinder = originalProvider.GetBinder(configuration, modelType);

        if (originalBinder != null)
        {
            return new DogModelBinder();
        }

        return null;
    }
}

and using in controller something like,

public IEnumerable<string> Get([ModelBinder(typeof(DogModelBinder))] Dog dog)
{
    //controller logic
}
Jatin Parmar
  • 647
  • 4
  • 14