I had some problems to render my api response as xml in swagger, finally I got it to render in the correct format, application/xml with my first action below, but it still says I can only render it as application/json.
I tried to remove application/json from Produces attribute, but still shows only application/json.
Any ideas why its behaving in this way?
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddMvcOptions(o => o.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter()));
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = "";
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
{ "Bearer", Enumerable.Empty<string>() },
});
});
}
And action:
[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
var addressId = GetAddressIdByAddress(address);
string result = _soapApi.GetResultById(addressId);
return Content(result, "application/xml", Encoding.UTF8);
}
Same result with:
[Produces("application/json", "application/xml")]
public IActionResult GetByAddress(string address)
{
var addressId = GetAddressIdByAddress(address);
string result = _soapApi.GetResultById(addressId);
return Content(result, "application/xml", Encoding.UTF8);
}
Results in:
When clicking excecute I get the result in the correct XML-format though.
While this:
[Produces("application/json", "application/xml")]
public string GetByAddress(string address)
{
var addressId = GetAddressIdByAddress(address);
string result = _soapApi.GetResultById(addressId);
return result;
}
Or this:
[Produces("application/json", "application/xml")]
public List<Address> GetByAddreses()
{
string result = _soapApi.GetResults();
return result;
}
Results in:
I am not able to return a list of parsed objects due to different data-structure returned for different parameters. So at this point I only receive raw soap xml-data and have to parse/deserialize it. But to actually be able to see the raw response I also need to show it as Content from string with content-type application/xml. And it gives me a nice output like (even though it still says only application/json is possible):
To summarize it:
I did not get it work with response content-type showing the correct content-type (in "Response Content Type" -filter) when a string was parsed to its actual format like below. Even though this results in the correct output for application/xml that was the most important (see screenshot above);
[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
return Content("<xml>...</xml>", "application/xml", Encoding.UTF8);
}


