I'm (trying) to upgrade ASP.NET Core application from .NET Core App 3.1 to .NET 6 but one test fails that deserialize a Problem result. Reason for failing is that in .NET 6 the content type is application/problem+json whilst in .NET Core App 3.1 application/xml.
Have searched for any notes regarding this in migration document but can't find anything.
A repro is available in my GitHub and the controller is very simple
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
namespace ProblemDetailsXMLSerialization
{
[ApiController]
[Route("[controller]")]
public class XmlController : ControllerBase
{
[HttpPost]
[Produces(MediaTypeNames.Application.Xml)]
[Consumes(MediaTypeNames.Application.Xml)]
public IActionResult Xml()
{
return Problem();
}
}
}
// Test file
using Microsoft.AspNetCore.Mvc.Testing;
using ProblemDetailsXMLSerialization;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace TestProject1
{
public class UnitTest1
{
[Fact]
public async Task Test1()
{
// Arrange
var application = new WebApplicationFactory<Startup>();
var client = application.CreateClient();
// Act
const string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";
var content = new StringContent(xml, Encoding.UTF8, MediaTypeNames.Application.Xml);
var response = await client.PostAsync("xml", content);
// Assert
Assert.Equal(MediaTypeNames.Application.Xml, response.Content.Headers.ContentType.MediaType);
var responseString = await response.Content.ReadAsStringAsync();
}
}
}
Thanks
To get an
XMLresponse - matching your assert statement - you'll need to add anAcceptHTTP header with valueapplication/xml.From the documentation:
There are built-in strings for both
Acceptandapplication/xml.Setting that header to the
DefaultRequestHeadersmakes it being sent with every request made by thatHttpClientinstance.In case you only want/need it for a single request, then use a
HttpRequestMessageinstance.In either case, the
responseStringvariable will contain an xml payload similar to below one.