I have a simple ASP.Net Web API controller in an otherwise unchanged MVC4 Web API project that has a POST method that takes a Values class. When I do a:
POST /api/values with a body of { name: "somename" }
the Values() constructor gets called instead of the Values(string name) one. Normally, this isn't a problem because the Name property would have a public set and Web API would call it after construction. In this case it is private, so I get a default instance of Values.
If I remove the Values(int) ctor then it does call the Values(string) ctor.
Is there a reason ModelBinding isn't choosing the ctor with the name parameter?
Here's the example code:
using System.Web.Http;
namespace WebAPIPlayground.Controllers
{
public class ValuesController : ApiController
{
public void Post(Values value)
{
var a = value.ID; // == 0
var b = value.Name; // == null
}
}
public class Values
{
public int ID { get; private set; }
public string Name { get; private set; }
private Values() { }
public Values(int id) { ID = id; }
public Values(string name) { Name = name; }
}
}
I have already looked at: Routing and Action Selection and WebAPI Parameter binding under the hood among many other sites, but I do not understand this behavior.
intparameter).