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.

Paging defaults and validation

Paging defaults and validation - ASP.NET Core Tutorial

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

Start my 1-month free trial

Paging defaults and validation

- [Instructor] The client should be able to omit the offset or limit parameters when they retrieve a collection. Right now, if those parameters are omitted, the API will throw an exception. We can add some default values to fall back on if one or both of those parameters are missing. To add some paging defaults, let's jump over to appsettings.json first. In appsettings.json, at the bottom here let's say... We'll add a property called DefaultPagingOptions. And in here we'll say the default limit is 25 items, and the default offset is zero. Over in Startup.cs, let's pull this data into an instance of PagingOptions. Say services.Configure PagingOptions, and we'll pull this from Configuration .GetSection DefaultPagingOptions. Now over in the RoomsController, we'll hold onto a private PagingOptions called DefaultPagingOptions, and we'll inject this as an Ioptions from the options namespace. It'll be an Ioptions of PagingOptions, and when we inject it we'll say defaultPagingOptions equals…

Contents