Learn how to add versioning to ASP.NET Core MVC using prebuilt middleware. Explore how to install the middleware package, and add it to your application pipeline.
- [Instructor] To add versioning to this API, we need to add a new package. So we'll start in the package manager. I'm gonna search for the Microsoft.AspNetCore.Mvc.Versioning package and go ahead and install this. This package is flexible enough to support URL versioning, header or media type versioning, as well as other styles. Once the package is installed, I'll go to the startup class to configure it. We'll do this configuration in the configure services method.
I'll do services.AddApiVersioning and pass some options. First I wanna say that if I don't specify an API version, just assume the default version is new ApiVersion 1.0. I also need to specify where to find API version information, where to read it from. So I'll say options.ApiVersionReader equals new, we want the MediaTypeApiVersionReader.
Import this namespace from versioning. Next I wanna tell it that if I don't specify an API version, I want to assume the default version to be the one I specified earlier, 1.0. I want to report API versions true so that I'll get API version information on the responses. And finally, I need to specify a version selector, ApiVersionSelector, and we'll say that this is going to be the new CurrentImplementationApiVersionSelector.
And we need to pass this options instance to the current implementation API version selector. That'll do it for configuration, let's go over to the root controller. Let's say that we wanna mark this root controller as being version 1.0 of the API. We can add the API version attribute and say this is version 1.0. Now let's open up Postman and give it a test. If I send a request to the root controller, nothing changed about the request, but if I look at the returned headers, I'll see that it says, "API supported versions is 1.0 for this route." Now let me try modifying the request to request a different API version, since we're using media type versioning here, I'll say accept application JSON version equals 2.0.
This will result in an error, because we don't have an API version attribute on this route, that marks it as version 2.0. But if we did, we'd be able to request version 2.0 of this particular route. The way we've added versioning here is pretty unobtrusive. We're able to version the API if we need to, but otherwise the versioning system will stay out of the way.
- What is RESTful design?
- Building a new API with ASP.NET Core
- Using HTTP methods
- Returning JSON
- Creating RESTful routing with templates
- Securing RESTful APIs with HTTPS
- Representing resources
- Representing links
- Representing collections
- Sorting and searching collections
- Building forms
- Adding caching to an ASP.NET Core API
- Configuring user authentication and authorization
Skill Level Intermediate
1. REST API Concepts
2. Build a Basic API
3. Versioning and Errors
4. Secure the API
5. Represent Resources
6. Represent Links
7. Represent Collections
Add pagination7m 37s
8. Sorting Collections
9. Searching Collections
10. Forms and Modifying Data
11. Caching and Compression
12. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.