C
C#7mo ago
altyn

ASP.NET get requests

Currently, I am fetching everything that can be paged through GET requests. Initially, it was just the page and pageSize, but I have added sorting so that it can sort by any property and multiple properties at once (one after the other). Here, the obvious limitations of retrieving things from the Query started to become apparent. Now, I am trying to add filtering, but it will be impossible to push it into query parameters without complicated parsing. I have circled in purple how I solved the problem with OrderBy and ascending. Do you know a better solution for this?
No description
2 Replies
Angius
Angius7mo ago
You could encapsulate all of your query params in a class, and have that as the parameter to the method
public sealed record FilterParams(SortBy SortBy, SortDirection SortDirection, string Name, int Page, int PerPage, ...)
public sealed record FilterParams(SortBy SortBy, SortDirection SortDirection, string Name, int Page, int PerPage, ...)
... OnGetAsync([FromQuery] FilterParams @params)
{

}
... OnGetAsync([FromQuery] FilterParams @params)
{

}
Alternatively, use a library like Sieve to handle the filtering and sorting for you
altyn
altyn7mo ago
Thanks, I will try