Angularjs 从Angular发布到.net WebAPI始终为空

Angularjs 从Angular发布到.net WebAPI始终为空,angularjs,asp.net-web-api,Angularjs,Asp.net Web Api,我试图将一个对象从AngularJS发布到MVC5WebAPI控制器,但该值始终为空 我可以在Chrome开发工具中看到数据可以在请求中找到 角度控制器: $scope.join = function () { if (!$scope.joinForm.$valid) return; // Writing it to the server var payload = $scope.user; var res = $http.post('/api/some',

我试图将一个对象从AngularJS发布到MVC5WebAPI控制器,但该值始终为空

我可以在Chrome开发工具中看到数据可以在请求中找到

角度控制器:

$scope.join = function () {
    if (!$scope.joinForm.$valid) return;

    // Writing it to the server
    var payload = $scope.user;

    var res = $http.post('/api/some', JSON.stringify( { Data: { payload }  }), { header: { 'Content-Type': 'application/json' } });
    res.success(function (data, status, headers, config) {
        $scope.message = data;            
    });
    res.error(function (data, status, headers, config) {
        alert("failure message: " + JSON.stringify({ data: data }));
    });
}
public class SomeController : ApiController
{        
    // POST api/values 
    public void Post([FromBody]string value)
    {           
        Trace.Write(value);
    }
}
MVC 5 API控制器:

$scope.join = function () {
    if (!$scope.joinForm.$valid) return;

    // Writing it to the server
    var payload = $scope.user;

    var res = $http.post('/api/some', JSON.stringify( { Data: { payload }  }), { header: { 'Content-Type': 'application/json' } });
    res.success(function (data, status, headers, config) {
        $scope.message = data;            
    });
    res.error(function (data, status, headers, config) {
        alert("failure message: " + JSON.stringify({ data: data }));
    });
}
public class SomeController : ApiController
{        
    // POST api/values 
    public void Post([FromBody]string value)
    {           
        Trace.Write(value);
    }
}
如果我在{Data:{payload}中包装我的对象

{"Data":{"payload":{"symbol":"xxx","amount":12000,"startdate":"2014-05-23T14:26:54.106Z","enddate":"2015-05-23T14:26:54.106Z","interval":"7 minutes"}}}
如果我不包装它,我会得到:

{"symbol":"xxx","amount":12000,"startdate":"2014-05-23T14:26:54.106Z","enddate":"2015-05-23T14:26:54.106Z","interval":"7 minutes"}
(Visual Studio 2015配置为使用IISExpress)


有什么想法吗?

值为空的原因是框架的模型绑定器无法将参数与文章正文中发送的数据匹配

创建一个类来存储有效负载

public class User
{
    public string symbol { get; set; }
    public int amount { get; set; }
    public DateTime startdate { get; set; }
    public DateTime enddate { get; set; }
    public string interval { get; set; }
}
更新控制器

public class SomeController : ApiController
{        
    // POST api/post
    public void Post(User user)
    {           
        //consume data
    }
}
$scope.join = function () {
    if (!$scope.joinForm.$valid) return;

    // Writing it to the server        
    var res = $http.post('/api/some', $scope.user, { header: { 'Content-Type': 'application/json' } });
    res.success(function (data, status, headers, config) {
        $scope.message = data;            
    });
    res.error(function (data, status, headers, config) {
        alert("failure message: " + JSON.stringify({ data: data }));
    });
}
角度控制器

public class SomeController : ApiController
{        
    // POST api/post
    public void Post(User user)
    {           
        //consume data
    }
}
$scope.join = function () {
    if (!$scope.joinForm.$valid) return;

    // Writing it to the server        
    var res = $http.post('/api/some', $scope.user, { header: { 'Content-Type': 'application/json' } });
    res.success(function (data, status, headers, config) {
        $scope.message = data;            
    });
    res.error(function (data, status, headers, config) {
        alert("failure message: " + JSON.stringify({ data: data }));
    });
}

首先,脚本中的url指向与您为控制器/操作组合提供的url不同的urlhere@DanPantry
POST-api/some
会击中
some控制器。POST
我同意,但在执行此操作时,还需要简单地发布$scope.user as,而不是将其转换为json字符串。我对angular不够熟悉,无法确认这一点。@jbrown,我刚刚检查过,您是正确的。很好,谢谢。我的原始答案中有一个错误,并且已经更新了。创建一个类并发布$scope.user而不是有效负载对象成功:-)