我正在尝试调用 POST API 控制器。控制器被调用,但复杂对象为空。我已经运行了 Fiddler,并且该对象甚至来自那里。我究竟做错了什么?
我的 C# 对象
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 后控制器
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代码
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 路由配置
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "RegisterApi",
routeTemplate: "api/{controller}/{user}"
);
}
}