Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON对象发布到C#ASP.NET WEB API_C#_.net_Angularjs_Json_Http - Fatal编程技术网

将JSON对象发布到C#ASP.NET WEB API

将JSON对象发布到C#ASP.NET WEB API,c#,.net,angularjs,json,http,C#,.net,Angularjs,Json,Http,我无法在HTTP消息体中发布JSON对象 我甚至试过这个 $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 但它仍然不起作用 这是我的密码: <!DOCTYPE html> <html lang="en"> <script src="https://ajax.googleapis.com/ajax/libs

我无法在HTTP消息体中发布JSON对象

我甚至试过这个

$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
但它仍然不起作用

这是我的密码:

<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <form novalidate>
    type:<br>
    <input type="number" ng-model="config.type"><br>
    Id:<br>
    <input type="text" ng-model="config.Id">
    <br><br>
    <button ng-click="submit()">Submit</button>
  </form>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope,$http) {
    $scope.config = {type:0, Id:"786"};
    $scope.submit = function() {
        var data = $scope.config;
        //$http.post("http://localhost:8612/api/values/", data);
        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "http://localhost:8612/api/values/", true);
        xhttp.setRequestHeader("Content-type", "application/json");
        xhttp.send(JSON.stringify(data));
        alert("Done");
    };
});
</script>

</body>
</html>

所以反序列化可能失败了。将请求模型设置为
对象
类型是错误的;相反,您应该定义一个合适的请求模型,并像这样使用它

public class ApiRequestModel 
{
   // define all your required properties client going to send
}

[HttpPost]
public IActionResult Post([FromBody] ApiRequestModel c)

所以反序列化可能失败了。将请求模型设置为
对象
类型是错误的;相反,您应该定义一个合适的请求模型,并像这样使用它

public class ApiRequestModel 
{
   // define all your required properties client going to send
}

[HttpPost]
public IActionResult Post([FromBody] ApiRequestModel c)