Controller action is not readable. JSON POST Guide

I have a Controller action declared as:

[Route("api/person")]
[HttpPost]
public async Task<IActionResult> Person([FromBody] Guid id) { ... }

I send him:

POST /api/person HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 747a5d76-398c-e1c7-b948-b276bb24976c

{
    "id": "b85f75d8-e6f1-405d-90f4-530af8e060d5"
}

My action strikes, but the Guidone it receives is always the value Guid.Empty(that is: it does not receive the value that I pass to it).

Notice this works fine if I use url parameters instead [FromBody], but want to use the http body of the message instead.

+4
source share
1 answer

As described in the Web API Documentation :

By default, the Web API uses the following rules to bind parameters:

  • "" , Web API URI. .NET(int, bool, double ..), TimeSpan, DateTime, Guid, decimal, string, , . ( .)
  • Web API -.

[FromBody] , [FromBody] , , , . catch-sample , , JSON.

, 2 :

JSON

POST /api/person HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 747a5d76-398c-e1c7-b948-b276bb24976c

"b85f75d8-e6f1-405d-90f4-530af8e060d5"

- :

public class Request
{
    public Guid Id { get; set; }
}

[Route("api/person")]
[HttpPost]
public async Task<IActionResult> Person([FromBody] Request request) { ... }
+6

All Articles