Web API Post parameters are always Null

二次信任 提交于 2019-12-06 07:35:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!