Sorting information is usually received via query parameters. The POCO SortingParams simply hold this information and passes to service (or repository).
Problem
How to implement sorting in ASP.NET Core Web API.
Solution
Create an empty project, add NuGet package:
Update the Startup class to add services and middleware for MVC.
Add model to hold the sorting data.
Add an extension method to IQueryable.
Add output models to send the data via API.
Add a service and domain model.
Add a controller for the API with service injected via constructor.
Output
Discussion
Let’s walk through the sample code step-by-step.
Note The sample is a very basic implementation of sorting. You will get a ParseException if the field specified for sorting doesn’t exist on the model. Typically, you would either verify field’s existence or catch the exception.
Source Code
GitHub
Pro WPF: Windows Presentation Foundation in .NET 3.0