【问题标题】:POSTing object from AngularJS to .net WebAPI is always null从 AngularJS 到 .net WebAPI 的 POST 对象始终为空
【发布时间】:2018-11-18 18:52:20
【问题描述】:

在调试 web api post 操作的值始终为空时,我尝试了所有方法。 我尝试更改标题,我在发布的模型类上方添加了 [JsonObject(MemberSerialization.OptOut)],我尝试了一个简单的 Dto。没有任何工作... 这是传递用户数据的控制器函数:

$scope.Add = function () {
        var user = {
            ID: $scope.ID,
            FirstName: $scope.FirstName,
            LastName: $scope.LastName,
            Age: $scope.Age,
            Address: $scope.Address
        };
        $scope.usersRepo.push(user);
        myService.addUser(user);

服务功能是:

var addUser = function (user) {
        return $http.post(
            usersApi + '/addNewUser',
            JSON.stringify(user)),
            {
                headers: {
                    'Content-Type': 'application/json'
                }
            };
    };

web api 操作是:

[HttpPost, ActionName("addUser")]
    public void Post([FromBody]UserModel value)
    {
        UsersDB.Add(value);
    }

我的模型是这样的:

[JsonObject(MemberSerialization.OptOut)]
public class UserModel
{
    public UserModel()
    {

    }
    public UserModel(string firstName, string lastName, int age, string address)
    {
        this.Address = address;
        this.Age = age;
        this.FirstName = firstName;
        this.LastName = lastName;
    }

    public void ConvertDto(UserDto userDto)
    {
        ID = userDto.ID;
        FirstName = userDto.FirstName;
        LastName = userDto.LastName;
        Age = userDto.Age;
        Address = userDto.Address;
    }

    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    }

【问题讨论】:

  • 通常不需要对 POST 数据进行字符串化,因为 AngularJS $http 服务会自动执行此操作。还需要添加application/json 标头,因为这是默认的。
  • 那是我的出发点。
  • 我发表评论是为了让其他人不会复制不必要的代码。

标签: angularjs http post asp.net-web-api service


【解决方案1】:

像这样使用http post方法。 data 应该是您要在没有 json stringify 的情况下传递的对象。我建议你为 http 方法创建一个服务。

 var obj = {
        url: your url,
        async: true,
        method: 'POST',
        headers: {
            "content-type": "application/json; charset=utf-8",
        }
    };
    if (typeof data != 'undefined' && typeof data != null) {
        obj.data = data;
    }
    $http(obj).then(function() {}, function() {});

服务: // http 方法

app.service('MethodProvider', ['$http', function ($http) {
    var self = this;
    self.get = function (url, data) {
        var obj = {
            url: url,
            async: true,
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        if (typeof data != 'undefined' && data != null) {
            obj.params = data;
        }
        return $http(obj);
    };
    self.post = function (url, data) {
        var obj = {
            url: url,
            async: true,
            method: 'POST',
            headers: {
                "content-type": "application/json; charset=utf-8",
            }
        };
        if (typeof data != 'undefined' && typeof data != null) {
            obj.data = data;
        }
        return $http(obj);
    };
     self.put = function (url, data) {
        var obj = {
            url: url,
            async: true,
            method: 'PUT',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        if (typeof data != 'undefined' && data != null) {
            obj.data = JSON.stringify(data);
        }
        return $http(obj);
    };
    self.delete = function (url) {
        var obj = {
            url: url,
            async: true,
            method: 'DELETE',
            headers: {
                'Content-Type': 'application/json'
            }
        };
        if (typeof data != 'undefined' && data != null) {
            obj.data = JSON.stringify(data);
        }
        return $http(obj);
    };
}]);

【讨论】:

  • 您是否将用户分配给 obj.data ?
  • 谢谢!它仅在我编写 obj.data = user 时才有效。不适用于 obj.user=user。与之前的解决方案相同:如果我发送一个变量名数据,它就可以工作。你知道这是为什么吗?
  • 表达式typeof data != 'undefined' || typeof data != null 总是true。你用这个表达的意图是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
相关资源
最近更新 更多