From the course: Building and Securing RESTful APIs in ASP.NET Core

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Validate sort parameters

Validate sort parameters - ASP.NET Core Tutorial

From the course: Building and Securing RESTful APIs in ASP.NET Core

Start my 1-month free trial

Validate sort parameters

- [Instructor] When a request comes into the API, ASP.NET Core MVC will bind any OrderBy parameters from the query string to the OrderBy property. Because the sort options model implements the i-validatable object interface, MVC will automatically call the validate method. We can validate the sort terms here, and let MVC know if the parameters passed by the client were valid. To keep the code clean, we'll delegate this to a new class called SortOptionsProcessor. This also needs to be declared as T and TEntity, and we'll pass in the OrderBy parameter. Now let's go create SortOptionsProcessor. We'll create this in the Infrastructure folder. This will be SortOptionsProcessor of T, comma, TEntity. This class will hold a private reference to the OrderBy string array, and it'll accept that parameter in the constructor. First, we'll declare a method that returns an IEnumerable of a new class called SortTerm, and we'll call this GetAllTerms. Before we continue, let's create that SortTerm…

Contents