Take advantage of FormatFilterAttribute in ASP.NET Core to format response data based on the request URL. Credit: Simon Carter / Getty ASP.NET Core provides excellent support for formatting data, whether it’s output data or data in the request body. When working in ASP.NET Core, your API methods will typically return JSON data, but you can also return data in formats other than JSON such as XML. For example, a call to /api/default.json should return data from the API method in JSON format, whereas a call to /api/default.xml should return the data in XML format. In this article, we’ll look at how we can control the format in which data will be returned from ASP.NET Core based on the extension of the URL. To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here. Create an ASP.NET Core API project First off, let’s create an ASP.NET Core API project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.Net Core API project in Visual Studio 2019. Launch the Visual Studio IDE. Click on “Create new project.” In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed. Click Next. In the “Configure your new project” window shown next, specify the name and location for the new project. Click Create. In the “Create New ASP.NET Core Web Application” window, select .NET Core as the runtime and ASP.NET Core 2.2 (or later) from the drop-down list at the top. I’ll be using ASP.NET Core 3.0 here. Select “API” as the project template to create a new ASP.NET Core API application. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here. Ensure that Authentication is set as “No Authentication” as we won’t be using authentication either. Click Create. You should have a new ASP.NET Core API project ready to go in Visual Studio. Next select the Controllers solution folder in the Solution Explorer Window and click “Add -> Controller…” to create a new controller named DefaultController. We’ll use this project and controller in the subsequent sections of this article. Install the XML formatter NuGet package for ASP.NET Core Now that we have created an ASP.NET Core application in Visual Studio, the next thing you should do is install the necessary NuGet package(s). Since ASP.NET Core is modular, you will need to add any functionality explicitly. To add support for the XML formatter, you should add the Microsoft.AspNetCore.Mvc.Formatters.Xml NuGet package. You can install this package from the NuGet Package Manager window in Visual Studio 2019. Alternatively, you can write the following command to install this package via the .NET CLI. dotnet add package Microsoft.AspNetCore.Mvc.Formatters.Xml Use the FormatFilter attribute in ASP.NET Core You can take advantage of the FormatFilter attribute to specify the output format via the URL. The following code snippet shows how you can include the {format} parameter to achieve this. [HttpGet("{id}.{format?}")] public string Get(int id) { return "The value is: "+id.ToString(); } Now that the FormatFilter attribute has been applied to the action method, you can test your API method using the JSON formatter as shown below: http://localhost:5239/api/default/10 or http://localhost:5239/api/default/10.json Since JSON formatter is registered with the pipeline by default and the XML formatter has not been added and configured yet, an attempt to test your API method using the following URL will result in an HTTP 404 error. http://localhost:5239/api/default/10.xml Here is the complete source code of the DefaultController class for your reference: [Route("api/[controller]")] [ApiController] [FormatFilter] public class DefaultController : ControllerBase { [HttpGet("{id}.{format?}")] public string Get(int id) { return "The value is: "+id.ToString(); } } Add the XML serialization formatters method in ASP.NET Core You can leverage the AddXmlSerializerFormatters() method to add support for XML serialization formatters for both input and output data as shown in the code snippet given below. public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddXmlSerializerFormatters(); } You can also call the AddXmlSerializerFormatters() method as shown below. public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddXmlSerializerFormatters(); } If you would like to format the output as XML but not read XML from the request body, you can write the following code in the ConfigureServices method of the Startup class. services.AddMvc(options => { options.OutputFormatters.Add(new XmlSerializerOutputFormatter()); }); Finally, run the application and browse to the following URL: http://localhost:5239/api/default/10.xml The output should appear as shown in the screen image below. IDG Figure 1: XML formatter in action! The FormatFilterAttribute can read the format to be used from the URL in ASP.NET Core. Once you have applied this attribute to your action methods, you can test your action methods using the formatters available by specifying the formatter to be used in the suffix of the URL. The AddXmlSerializerFormatters() extension method enables you to add both the input and output XML formatters to the request processing pipeline in ASP.NET Core. Note that besides XML and JSON formatters, you can use custom formatters as well. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos