Admiral Land
Admiral Land

Reputation: 2492

Binding Guid in Controller method in Asp.Net Core

I have controller method:

    [EnableCors("MyPolicy")]
    [Route("page/{session:guid}")]
    [HttpGet("page")]
    public async Task<ActionResult<DtoResult>> GetPageAsync(Guid session, int page = -1,  int pageSize = -1)
    {
            var result = await _paginationInfrastructure.GetDtos(session, page, pageSize, _reportManagerService);
            return Ok(result);
    }

Angular client http code:

getPage(sessionGuid: string, pageIndex: number, pageSize: number): Observable<ReportDtoResult> {
var paginationUrl = this.configService.getPaginationUrl();
var fullPaginationUrl = `${paginationUrl}`;
  
var paramsArgs = new HttpParams();
paramsArgs.set('session',sessionGuid);
paramsArgs.set('page',pageIndex);
paramsArgs.set('pageSize',pageSize);
return this.http.get<DtoResult>(fullPaginationUrl,{params:paramsArgs});

}

request via Swagger:

curl -X GET "https://localhost:5001/page/081B1B5A-9875-4813-B5ED-A131598B3FC4?page=-1&pageSize=-1" -H "accept: text/plain"

So, when send Guid string from client to server i get empty guid. I try to make routing , but it not work for me.

How to make valid request?

P.S. the request must be:

var fullPaginationUrl = `${paginationUrl}/${sessionGuid}?page=${pageIndex}&pageSize=${pageSize}`;

And it works! Thanks!

Upvotes: 0

Views: 1735

Answers (1)

Shahzad
Shahzad

Reputation: 157

The problem isn't with your route setup, it's with the way you're making the request in Angular.

You're adding the guid as a query parameter named session. What you need to do instead is to make it part of the path (as you've got configured).

So you request should look like the following:

...
var fullPaginationUrl = `${paginationUrl}/${sessionGuid}`;

var paramsArgs = new HttpParams();
paramsArgs.set('page', pageIndex);
paramsArgs.set('pageSize', pageSize);
...

The above assumes that paginationUrl is https://localhost:5001/page

The session in [Route("page/{session:guid}") is just specifying what the name of the parameter that you're mapping the guid to is in your action method

Upvotes: 1

Related Questions