Asp.Net Web Api PUT got a value with all null properties?

145 Views Asked by At

I have a big complex C# class

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<...>

And actions for GET and 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

In the Angular front-end, I have the following code to get the data

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

Then the following code send the data back.

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

However, the Visual studio debugger shows the parameter value of Put has null values for all the properties? id got the right value though. I tried to change the parameter definition to Put(string id, [FromBody]D2 value) but it's the same.

The following is the Angular service

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

Or is the service definition issue?


I created a new C# class

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

And I change the Javascript code to

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

But value still got null value for value.MyId in the Put method?

The following shows the raw Http requests.

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

There are 0 best solutions below