0

I'm working on a simple tutorial project using Angular 6 and ASP.NET Core WebAPI.

I'm not quite sure what i'm missing, but my POST calls don't work. To be precise, everything works in terms of communication with the web service, but the object that is passed into my controller method parameter has all its properties set to null or default value when null isn't admitted by C# types.

Here's my call in typescript:

createRequest(request: PrintRequestDto): Observable<PrintRequestDto> {
const actionUrl = this.apiBaseUrl + 'CreateRequest';

return this.http.post<PrintRequestDto>(actionUrl, JSON.stringify(request)); }

And this is my server-side C# method:

[Authorize]
[HttpPost]
[Route("CreateRequest")]
public PrintRequestDto CreateRequest(PrintRequestDto request)
{
    return _printServiceManager.CreateRequest(request);
}  

I already found this question here SO Question, but adding content-type specifications in my headers is not helping. Just to let you know, I'm using the HttpInterceptor built-in into Angular to inject JWT & content type informations in all my requests' headers.

As you can see here, they look properly set: enter image description here

I would also like to better understand the http features when it comes to these things, so if you have a link to some complete documentation to provide, it would be awesome.

2
  • As an aside, in ASP.NET Core you can combine the Route and HttpPost attributes (.i.e. [HttpPost("CreateRequest")]) if you prefer that. Commented Aug 23, 2018 at 13:49
  • Nice to know! Thank you! Commented Aug 23, 2018 at 13:52

1 Answer 1

4

You need to add [FromBody] attribute on the PrintRequestDto request

[Authorize]
[HttpPost]
[Route("CreateRequest")]
public PrintRequestDto CreateRequest([FromBody]PrintRequestDto request)
{
    return _printServiceManager.CreateRequest(request);
} 
Sign up to request clarification or add additional context in comments.

3 Comments

Working perfectly! Still don't know when to use those attributes :D Thank you! Have to wait some time but be sure i'll accept your answer
Ok. Thank you too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.