How to use output formatters in ASP.Net Core

Learn how to build a custom output formatter in ASP.Net Core to exchange data in formats other than JSON, XML, and plain text

ASP.Net Core provides built-in support for data exchange in the JSON and XML formats. If you want your ASP.Net Core application to transmit data over HTTP in a format that isn’t supported by one of these built-in formatters, then you will need to create a custom output formatter.

As an example, you might want to build a custom output formatter if you want to exchange data with clients using the more efficient Protobuf format. This article presents a discussion of what output formatters are, why they are needed, and how we can build a custom output formatter in ASP.Net Core.

Create an ASP.Net Core Web API project

First off, let’s create an ASP.Net Core Web API project in Visual Studio. If Visual Studio 2017 is up and running in your system, follow the steps given below to create an ASP.Net Core Web API project.

  1. Launch the Visual Studio 2017 IDE.
  2. Click on File > New > Project.
  3. Select “ASP.Net Core Web Application (.Net Core)” from the list of templates displayed.
  4. Specify a name for the project.
  5. Click OK to save the project.
  6. A new window “New .Net Core Web Application…” is shown next.
  7. Select .Net Core as the runtime and ASP.Net Core 2.1 or later from the drop-down list at the top of the window.
  8. Select “API” as the project template. 
  9. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we wn’t be using those features here. 
  10. Ensure that “No Authentication” is selected as we won’t be using authentication either. 

To continue reading this article register now