Trying out minimal APIs in .NET 6 and can’t make it work with XML content type. If I use standard controllers, using .AddXmlSerializerFormatters()
extension does the job:
builder.Services.AddControllers().AddXmlSerializerFormatters();
But when I switch from controller to .MapPost(..)
, I start getting 415 HTTP responses.
app.MapPost("/endpoint", ([FromBody] Request request) => {})
.Accepts<Request>("text/xml");
HTTP response: 415 Microsoft.AspNetCore.Http.BadHttpRequestException: Expected a
supported JSON media type but got "text/xml"
Is there any other way I can declare XML formatters that will work with minimal APIs?
2
Answers
As suggested by the post linked by guru-stron, it’s possible to pass XML documents by implementing your own wrapping model that provides a
BindAsync
method.I added a extension method to HttpRequest for convenient Content-Type validation.
You can then use the model directly as a paramter by your minimal API endpoint.
Some final thoughts: This implementation enables you to pass a generic XML document to the endpoint. However, if you expect a certain document structure, you could implement this by making the XDocumentModel expect a generic type parameter and extracting this type’s properties from the XDocument instance.
I did it this way: