How to receive requests with XML content in ASP.NET Core
By FoxLearn 3/1/2025 2:48:50 AM 53
Once the formatter is registered, when a request with an XML Content-Type (like application/xml) arrives, ASP.NET Core uses the InputFormatter to deserialize the XML content into a model.
In this article, I’ll guide you through the steps of receiving a request with XML by registering the built-in XML InputFormatter, defining a model class, and setting up the action method to handle the request.
There are two XML InputFormatters available: one that uses XmlSerializer
and one that uses DataContractSerializer
. For simplicity and ease of use, I’ll demonstrate using XmlSerializer
.
Register the XML InputFormatter
To start, you need to register the built-in XML InputFormatter in the initialization code. This is done by calling AddXmlSerializerFormatters()
.
using Microsoft.Extensions.DependencyInjection; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers().AddXmlSerializerFormatters(); // Additional setup code...
This line of code adds the XmlSerializerInputFormatter
to the collection of input formatters. It also configures the XML output formatter, which is needed if you plan to return XML responses.
Define a Model Class
Next, you’ll define a model class that corresponds to the XML structure you expect to receive.
public class Book { public string Title { get; set; } public string Author { get; set; } public int YearPublished { get; set; } }
The order of properties doesn’t matter when using XmlSerializer
, and no attributes are necessary for this to work.
Configure the Action Method
Now, you’ll create an action method that accepts the Book
model as a parameter. Optionally, you can use the [Consumes]
attribute to specify that this method only accepts application/xml
requests.
[HttpPost] [Consumes("application/xml")] // Optional: Restricts the request to XML only public IActionResult PostBook(Book book) { return Ok($"Received book: {book.Title} by {book.Author}"); }
Here, the Book
model will be automatically bound to the request body. You don’t need to use the [FromBody]
attribute because complex types like Book
are implicitly bound to the request body.
Note: The [Consumes("application/xml")]
attribute is optional. If you omit it, the API will accept both XML and JSON formats, making the endpoint more flexible. If you add it, the API will only accept requests with an XML Content-Type
.
Now, let’s send a request to the endpoint with an XML payload.
POST /Book
Content-Type: application/xml
Body:
<Book> <Title>The Great Gatsby</Title> <Author>F. Scott Fitzgerald</Author> <YearPublished>1925</YearPublished> </Book>
Once the request is sent, the registered XML InputFormatter will deserialize the XML data into the Book
object.
Received book: The Great Gatsby by F. Scott Fitzgerald
This shows how to receive XML content in an ASP.NET Core application by registering the XML InputFormatter, defining a model, and setting up an action method to handle the request. The deserialization is handled automatically as long as the XmlSerializerInputFormatter
is properly registered.