Creating custom OutputFormatter objects
In this example, our aim is to create a VCard output to be able to import the person's contact details directly into Microsoft Outlook or any other contact database that supports VCards. Later in this section, we also want to create a CSV output formatter.
Both are text-based output formatters, and they will derive their values from TextOutputFormatter
. Let's look at the steps to create the VCard output:
- Create a new class in a new file called
VcardOutputFormatter.cs
. - Now, insert the following class skeleton in the new file. You will find the implementations of the empty methods in the following code snippets. The constructor contains the supported media types and content encodings:
public class VcardOutputFormatter : TextOutputFormatter { public string? ContentType { get; } public VcardOutputFormatter() { ...