I am trying to call a POST API controller. The controller gets called but the complex object comes in empty. I have ran Fiddler and the object is even coming through populated there. What am I doing wrong?
My C# object
public class RegisterUser
{
public Guid PersonId { get; set; }
public string Email { get; set; }
public string Business { get; set; }
public string EmployeeNumber { get; set; }
public string UserName { get; set; }
}
API Post Controller
public HttpResponseMessage Post(RegisterUser user)
{
//This is where the problem is. Everything in user is null
//even though I can see it coming through on Fiddler.
}
Javascript code
function User(personId, userName, email, business, employeeNumber) {
this.PersonId = personId;
this.Email = email;
this.Business = business;
this.EmployeeNumber = employeeNumber;
this.UserName = userName;
}
function RegisterUser(url) {
var createdUser = new User("b3fd25ba-49e8-4247-9f23-a6bb90a62691", "username", "email", "business", "56465");
$.ajax(url, {
data: JSON.stringify({ user: createdUser }),
type: "post",
contentType: "application/json"
});
}
Web API Route Config
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "RegisterApi",
routeTemplate: "api/{controller}/{user}"
);
}
}