Angular json to.net core web api message is null (Updated)

I am trying to post some JSON in the web api.net core, but for some reason it is always null, no matter what I tried, see below.

this is my .net server-side web api code

    [Produces("application/json")]
    [Route("api/v1/Issues")]
    [Authorize]
    [EnableCors("AllowCors")]
    public class IssuesController : Controller
    {
        [HttpPost]
        public IActionResult PostIssues([FromBody] string issue)
        {
          //some code to desirialize
        }
    }

and this is how I try to send a message via angular (note that the corresponding services are loaded)

    public addIssue(issue) {
    const headers = new Headers();
    this.authService.getToken().toPromise().then( t => {
      headers.append('Authorization', `Bearer ${t.toString()}`);
      headers.append('Content-Type', 'application/json')
      this.http.post(`${this.baseUrl}/v1/issues`, issue, {
        headers: headers
      }).toPromise()
        .then(res => {
          console.log(res.json());
        });
    });
  }

I tried changing the message to

this.http.post(`${this.baseUrl}/v1/issues`, JSON.stringify(issue))

and even

this.http.post(`${this.baseUrl}/v1/issues`, {'issue', JSON.stringify(issue)})

but nothing works, does anyone know why this is happening? For the explanation string issueobtained in the API, there is always a null value. Here is a successful answer, when I get to the server, you can see that the request payload is not null, but reaches the web api as null enter image description here

1 , , 4.5 .net, , .net, , foo

public class Foo
    {
        public string Name { get; set; }
        public string Json { get; set; }
    }

,

   [HttpPost]
    public IActionResult PostIssues([FromBody] Foo issue)
    {
       Debug.Write(issue);
       return Ok(issue);
    }

angular

public addIssue(issue) {
const headers = new Headers();
this.authService.getToken().toPromise().then( t => {

  headers.append('Authorization', `Bearer ${t.toString()}`);
  headers.append('Content-Type', 'application/json');

  this.http.post(`${this.baseUrl}/v1/issues`, {'name' : 'name', 'json': JSON.stringify(issue)}, {
    headers: headers,
    withCredentials: true
  }).toPromise()
    .then(res => {
      console.log(res.json());
    });
});

}

enter image description here

, Json ? 4.5, .net?

+4
2

.

, API,

JSON , . ,

(viewmodel)

, asp json, , name. , , .

: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding

, , api, ?! string api,

FormData

api,

 public IActionResult PostIssues(string issue){..}

[FromBody].

 public addIssue(issue) {
    const headers = new Headers();
    this.authService.getToken().toPromise().then( t => {
      headers.append('Authorization', `Bearer ${t.toString()}`);
     // new code here
    var formData = new FormData();
    formData.append('issue',issue); //you need to specify the parameter name here.
      this.http.post(`${this.baseUrl}/v1/issues`, formData, {
        headers: headers
      }).toPromise()
        .then(res => {
          console.log(res.json());
        });
    });
  }

, .

+3

, URL

${this.baseUrl}/v1/issues?issue=yourVarIssue
0

Source: https://habr.com/ru/post/1683837/


All Articles