In this video, Nate explains how to add the Cache-Control header to specific API responses, and how to configure the header response.
- [Instructor] Before we go on, I wanted to point out that I updated the exercise files with my solution to the challenge posed at the end of the previous chapter. You can find the updated code in the Begin state of the exercise files for this video. If you've been following along throughout this course, you can copy the changes into your project. Or it may be easier to simply open the solution file in the Exercise Files folder, and continue from there. Alright, let's add caching to the API. ASP.NET Core uses the response cache attribute to indicate that a particular response is cachable.
To demonstrate this, let's add caching to the InfoController. On the GetInfo route, I'm going to add ResponseCache. We can set a duration in seconds. Since the data returned from this route is pretty static and doesn't change very often, I'll set a duration of one day. Now let's hit this route with Postman and see what we get back. I'll send a request to /info. And let's look at the headers. We have a Cache Control header with a max age of 86400, which is one day in seconds.
In this case, we're telling the client that this resource can be considered fresh in the cache, for at least one day before going stale. If you need to use the same ResponseCache settings in multiple places, you can set up a cache profile. We can do that over in Startup.cs. In the ConfigureServices method, and the AddMvc block, we can configure a cache profile. By saying opt.CacheProfiles.Add. We can make one called Static, and then create a new CacheProfile object with the values we need.
Over in the InfoController, instead of specifying the duration, we can simply say CacheProfileName = "Static". You should add the ResponseCache attribute to any response that you want the client to be able to cache.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Building Web APIs with ASP.NET Corewith Chris Woodruff1h 7m Intermediate
Deploying ASP.NET Core Applicationswith Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. 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.