2

I have created Web.Api based on .Net-Core. There are two methods:

  1. IEnumerable GetRoles() - (Get)
  2. ResponseObject GetUsers(PagerRequest pagerRequest) - (Post). It has the following attributes: [Route("api/users/getusers")] [HttpPost]

Both methods are called by Angular. Method GetRoles is working find but problem is in POST.

PagerRequest is a simple class which has a few int properties. It's filled in by Angular. Fiddler shows correct JSON:

POST http://localhost/Pir.API/api/users/getusers HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 84
Accept: application/json, text/plain, */*
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
content-type: application/json
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7

{
  "CountButtons": 5,
  "ItemsCount": 0,
  "PageSize": 5,
  "CurrentPageIndex": 0
}

GetUsers method is calling but input parameter is not initialized. In another words desirialization is not working and I cannot understand why.

Before this project was based on classic .Net and run in the IIS. It was working fine. But .Net CORE is a problems.

Who knows where the problem is?

Below code examples:

-------------- PagerRequest.cs --------------------

public class PagerRequest
{
     public int ItemsCount { get; set; }
     public int PageSize { get; set; }
     public int CurrentPageIndex { get; set; }
     public int CountButtons { get; set; }
 }

-------------- PagerRequest.ts --------------------

export class PagerRequest{
    ItemsCount:number;
    PageSize:number;
    CurrentPageIndex:number;
    CountButtons:number;    
}
0

2 Answers 2

6

You need to tell the framework to read the data from the request body. Do this with the FromBody Attribute

[HttpPost]
ResponseObject GetUsers([FromBody]PagerRequest pagerRequest)

Note: If performing a HttpPost, the name GetUsers is missleading. A tip is to change that

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It helped. Why was it working in classing application without this attribute?
@MikeTkachev Np, please accept answer :), In Asp.Net Web Api/MVC reading request data from the body was default in Controller methods.
0

1) You are missing the FromBody.

2) One tip while working on apis is to configure Swagger . Swagger creates a sample html by default where you can see what type of request body is needed from the UI frameworks (for example see this url https://petstore.swagger.io, all the api endpoints are visible as html by default and we can see the type of request needed).

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.