简体   繁体   English

http.post从angularJS到c#Web api的简单对象为null或所有属性为null

[英]http.post Simple object from angularJS to c# Web api either null or all properties are null

First off, I'll acknowledge there are lots of questions close to my one here, but having tried every solution I can find on SO I'm still stuck. 首先,我会承认这里有很多问题要解决,但是尝试了所有可以找到的解决方案后,我仍然陷入困境。

My Service method within my service.js is as follows, with comments; 我的service.js中的Service方法如下所示,并带有注释;

postSimpleObject: function () {

    // Have tried this first, and have passed
    // as JSON.stringify(simpleObject)
    var simpleObject = {
        name: "J Doe",
        colour: "Red"
    };

    // tried to pass this next
    var simplerObject = '{ "Name": "J Done", "Colour":"Red"}';

    // escaped the quotations and tried this next
    var simplerObject2 = '{ \"Name\": \"J Done\", \"Colour\":\"Red\"}';

    return $http.post(apiUrl + "PostSimpleObject?item=" + JSON.stringify(simpleObject), {
        headers: {
            'Content-Type': 'application/json'
        }
    });
}

Here is my API controller function on the API side; 这是我在API方面的API控制器功能;

public class CrudUserApiController : ApiController
{

    [System.Web.Http.HttpPost]
    public void PostSimpleObject(SimpleObject item)
    {
        var itemReceived = item;
    }    
}

my simple object class, on the api side; 我的简单对象类,在api端;

public class SimpleObject
{
    public string Name { get; set; }
    public string Colour { get; set; }

}

Now, what happens is; 现在,发生了什么;

  • The API method is triggered, the routing can locate the controller and method API方法被触发,路由可以找到控制器和方法
  • The model / object received is a new SimpleObject with null properties for both members 收到的模型/对象是新的SimpleObject,两个成员均具有null属性

As per the comments in the service, I've tried passing a stringified object, a json string and an escaped json string. 根据服务中的注释,我尝试传递一个字符串化的对象,一个json字符串和一个转义的json字符串。

Also on the API side I've tried using the [FromBody] attribute in front of the SimpleObject argument in the signature. 同样在API方面,我尝试使用签名中SimpleObject参数前面的[FromBody]属性。 The same thing happens. 发生同样的事情。

I'm totally lost, some help would be much appreciated. 我完全迷路了,不胜感激。 Thanks. 谢谢。

It would be advisable to post the content on the body instead of on the the querystring for a number of reasons, such as querystring length limitations. 出于多种原因(例如查询字符串长度限制),建议将内容发布在正文上而不是查询字符串上。

That said, if you insist on using the querystring, you need to tell WebAPI to look to the querystring for the data using the FromUri attribute, since the default is the body: 就是说,如果您坚持使用查询字符串,则需要告诉WebAPI使用FromUri属性查找数据的查询字符串,因为默认值为主体:

[System.Web.Http.HttpPost]
public void PostSimpleObject([FromUri]SimpleObject item)
{
    var itemReceived = item;
}    

Alternatively, you can post the content on the body directly as called out by ex0dm3nt: 或者,您可以按照ex0dm3nt的指示将内容直接发布到正文上:

$http.post(apiUrl + "PostSimpleObject", simpleObject);

You just need to pass your simpleObject as second parameter in the $post request like this: 您只需要像下面这样在$post请求simpleObject作为第二个参数传递:

postSimpleObject: function () {
    var simpleObject = {
        name: "J Doe",
        colour: "Red"
    };

    return $http.post(apiUrl + "PostSimpleObject", simpleObject);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM