我试图将
JSON数据发布到Web Api方法,但是JSON数据并不绑定到模型.
这是我的模型:
[DataContract] public class RegisterDataModel { [DataMember(Isrequired = true)] public String SiteKey { get; set; } [DataMember(Isrequired = true)] public String UserId { get; set; } [DataMember(Isrequired = true)] public String UserName { get; set; } }
这是我的Web Api操作:
public class RegisterController : ApiController { public Guid Post([ModelBinder] RegisterDataModel registerDataModel) { if (!ModelState.IsValid) { throw new ModelStateApiException(ModelState); } var userProfileDataContract = userProfileBusinessLibrary.GetNewOne(); userProfileDataContract.UserId = registerDataModel.UserId; userProfileDataContract.UserName = registerDataModel.UserName; var userKey = userProfileBusinessLibrary.Register(registerDataModel.SiteKey,userProfileDataContract); return userKey; } }
在添加[ModelBinder]之前,registerDataModel为null.添加[ModelBinder]后,registerDataModel是一个RegisterDataModel实例,但所有属性值都为空.
这是我通过Fiddler的请求:
http://local.testwebsite.com/api/register Request Headers: User-Agent: Fiddler Host: local.testwebsite.com Content-Length: 89 Content-Type: application/json; charset=utf-8: Request Body: { "SiteKey":"qwerty","UserId": "12345qwerty","UserName":"john q" }
我发现我的帖子数据绑定到RegisterDataModel属性是什么?谢谢你的帮助.