问题
did anybody find a proper resolution of this? I just tried with WebAPI in big hopes (after completing a successful implementation of rest api in php/restler with KO and HTML5). Get is working like charm. Got stuck in post for last 20+ hours as no matter what always my variable is null. I do have DTO and repository implementation which is part of my framework anyway. I tried with FromBody hint and pretty much everything available in my search. I am testing this using CRest and Fiddler. I am even fine with getting raw request.content and then will deal with json loading etc by myself. This point I am just frustrated and thinking about moving back to 4.0 and use wcf to generate ResT again. Any help would be really appreciated ...
DTO:
public class TestEntity
{
public TestEntity() { }
public int UserId { get; set; }
public string UserName { get; set; }
}
Controller: public class testController : ApiController
{
// POST api/test
public TestEntity PostTest([FromBody] TestEntity t)
{
var x = new TestEntity();
if (!this.ModelState.IsValid)
{
return x;
}
else
{
return x;
}
}
I am invoking thru CREST ....
回答1:
Your issue came when you calling api using xml type i think,
Web API using Json Serializer in default hence your application working after you changing the content-type to JSON.
See the below code and add this to your WebApiConfig.cs , it is using to handle XML type request.
config.Formatters.XmlFormatter.UseXmlSerializer = true;
And call again your service.It will fix this issues.
来源:https://stackoverflow.com/questions/12690514/web-api-post-parameters-are-always-null