Kshitiz Shakya
Kshitiz Shakya

Reputation: 119

Questions about Web API DefaultModelBinder Behaviour

Given, routes are set up like below

For MVC : /entries/{entryID}/Children

For API: /api/entries/{entryID}/Children

The model I want the request to bind to is.

 public class Entry
 {
    public int EntryID {get;set;}
    public string EntryName{get;set;}
    public string Count{get;set;}
 }

And controller like below,

For MVC,

public ActionResult Create(Entry entry)

For Web Api,

public IHttpActionResult Create(Entry entry)

In both cases, EntryName and Count are provided via the form data. But I want to the EntryID to be retrieved via the url i.e the route data.

Only, the MVC Default Model Binder is able to bind the value of EntryID from the route data. But, Default Model Binder in Web API is unable to bind the value of property EntryID from the route data.

According to Pro ASP.NET MVC 5 Book, DefaultModelBinders look for any value in the following location in the given order.

  1. Request.Form
  2. RouteData.Values
  3. Request.QueryString
  4. Request.Files

Is this also for the Web API DefaultModelBinder? How do i get the entryID in WebAPI DefaultModelBinder to bind entryID from the route data.

Upvotes: 1

Views: 84

Answers (0)

Related Questions