angularjs - POSTing object from AngularJS to .net WebAPI is always null
问题描述
I tried everything yet still when debugging the value at the web api post action is always null. i tried changing the headers, i added [JsonObject(MemberSerialization.OptOut)] above the posted model class, i tried a simple Dto. nothing works... this is the controller function that passes the user data:
$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);
the service function is:
var addUser = function (user) {
return $http.post(
usersApi + '/addNewUser',
JSON.stringify(user)),
{
headers: {
'Content-Type': 'application/json'
}
};
};
and the web api action is:
[HttpPost, ActionName("addUser")]
public void Post([FromBody]UserModel value)
{
UsersDB.Add(value);
}
my model is this:
[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; }
}
解决方案
像这样使用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);
};
}]);
推荐阅读
- javascript - 如何使和尚 findoneandupdate 属性成为变量?
- c# - Linq 反射表达式对属性的属性抛出异常
- c++ - 无法在函数 C++ 中打印二维数组
- python-3.x - 使用opencv播放视频时延迟?
- python - 表达式函数类型执行不正确
- java - Eclipse Luna 首选项页面上的 PyDev 错误
- python - 绘制对数似然函数作为 theta1 和 theta2 的函数
- laravel - whereHas 和 whereDoesntHave 在 laravel eloquent 中没有按预期工作
- tensorflow - 不允许迭代 `tf.Tensor`:AutoGraph 确实转换了这个函数。这可能表明您正在尝试使用不受支持的功能
- rust - 如何在 Rust 中保存子图像?