Asp.Net Web Api PUT获得了一个具有所有null属性的值?

时间:2016-12-01 00:43:13

标签: asp.net angularjs asp.net-web-api angularjs-resource

我有一个很复杂的C#类

public class D2 {
    [Key] public string Id { get; set; }
    // ..... Many other properties with type of string, DateTime, Int16?....
    public List<Ref1> R1 { get; set; }
    // ..... several other properties with type of List<...>

GET和PUT的行动

[HttpGet("{id}")]
public async Task<IActionResult> Get(string id)
{
    var d = await _ctx.D2
        .include(e => e.RefA).ThenInclude(e => e.RefA1)
        .include(e => e.RefB).ThenInclude(e => e.RefB2)
        ......
    return OK(d);
 }

[HttpPut("{id}")]
public async Task<IActionResult> Put(string id, D2 value)
{
    // value suppose to be passed from the web front-end

在Angular前端,我有以下代码来获取数据

Service.get({ id: model.id }).$promise.then(function (x) { model.d2 = x; });

然后以下代码将数据发回。

console.log(model.d2); // d2 has property values
DealService.update({ id: model.id, value: model.d2});

但是,Visual Studio调试器显示value的参数Put对所有属性都有空值? id虽然得到了正确的价值。我试图将参数定义更改为Put(string id, [FromBody]D2 value),但它是相同的。

以下是Angular服务

.factory('Service', ['$resource', function ($resource) {
    return $resource('http://localhost:5001/api/D2/:id',
        { id: '@id' },
        {
            update: { method: 'PUT' }
        });
}])

或者是服务定义问题?

我创建了一个新的C#类

public class DPost
{
    [Key]
    public String MyId { get; set; }
} 

我将Javascript代码更改为

var m = {};
m.myId= 'test';
console.log(m);
DealService.update({ id: model.id, value: m});

value.MyId方法中Put的值仍为空值

以下显示原始Http请求。

PUT http://localhost:5001/api/D2/B161 HTTP/1.1
Host: localhost:5001
Connection: keep-alive
Content-Length: 52
Accept: application/json, text/plain, */*
Origin: http://localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:8082/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8

{"id":"B161","value":{"myId":"B161"}}

0 个答案:

没有答案