The ION specification describes a unique media type that identifies ION content. In this video, Nate demonstrates adding a custom media type to ASP.NET Core MVC.
- [Instructor] ASP.NET Core automatically attaches some appropriate headers to the response. If we check the headers in Postman, we can see that it's attaching the content type application/json. This is because ASP.NET Core is seeing that we're returning a JSON response. The ION specification describes the ION media type as applicaton/ion+json. To keep our API in line with the ION spec, we can change the media type that's returned. You don't have to do this in your API, but it's a nice way to let clients know you are following the official ION spec.
I'm going to stop the project. And I've added a folder called Infrastructure to hold some additional code files. And I've added a class called ION Output Formatter. I'm going to hide the Solution Explorer so that I have more room here. This class wraps up the default JSON Output Formatter that ships with MBC, but just changes the media type to application/ION+JSON. To make this work in the application, it needs to be hooked up in the start up class. So in the startup.cs file, in the configure services method, we'll overload the add Mvc call to provide some additional options to Mvc.
One of the options that you can configure for Mvc is called Output Formatters. The Output Formatters collection contains all the classes that can format the output of a response. So, the default Output Formatters are: JSON Output Formatter, TEXT Output Formatter, and so on. We need to grab a references to the JSON Output Formatter that's in the collection by default. So I can use Linq and just say "Of type JSON Output Formatter". You need to pull in the name space that this belongs in and then just do single to pull it out of the collection.
Next I need to actually remove it from the collection. This is because if it's still in there, Mvc will just use the JSON Output Formatter as the default. We want our ION Output Formatter to be used instead. So now we can add an instance of our ION Output Formatter, this is also in another name space. And because this just delegates to the JSON Output Formatter, we need to pass in the reference we pulled out before. I missed the add method. Add and that should do it.
So now if we save and run this application again and hit it with Postman, we should see the exact same response but a media type of application/ion+json instead. And there we go. Next, we'll discuss some of the approaches you can use to version your API.
- 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 Core (2016)with 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.