I have a string that is XML and I need to return it as an XML document. By default, this is returned with the content type of text/plain
. The content is rendered, but I need the content type to be application/xml
. I've enabled the option RespectBrowserAcceptHeader, which will serialize objects as XML and set the correct content type, except if the object is a string.
[HttpGet]
public string Get()
{
return xmlString;
}
public static string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<sample>
Hello World.
</sample>";
the Web API 2 framework defaults to XML but uses the client's accept header to determine the return type format. If XML is required for every response then configure Web API to always return XML. This should NOT be done in the action since it is just configuration. ASP.NET Core defaults to JSON.
The REST API Client Service currently accepts only JSON input when making REST API Create, Read, Update, or Delete requests. It is nevertheless possible to use XML input when making REST API requests.
Some common return types in this category are BadRequestResult (400), NotFoundResult (404), and OkObjectResult (200). Alternatively, convenience methods in the ControllerBase class can be used to return ActionResult types from an action.
If you have a string that is XML and need to return it as an XML document, then return a ContentResult.
[HttpGet]
public ContentResult Get()
{
return new ContentResult
{
ContentType = "application/xml",
Content = xmlString,
StatusCode = 200
};
}
Controller
using Microsoft.AspNetCore.Mvc;
namespace MyXmlSample
{
[Route("xml")]
public class MyXmlController
{
public static string xmlString =
@"<?xml version=""1.0"" encoding=""UTF-8""?>
<sample>
Hello World.
</sample>";
[HttpGet]
public ContentResult Get()
{
return new ContentResult
{
ContentType = "application/xml",
Content = xmlString,
StatusCode = 200
};
}
}
}
Startup
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace MyXmlSample
{
public class Program
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Program>()
.Build();
host.Run();
}
}
}
project.json
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNetCore.Mvc.Core": "1.0.0-*",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*",
"Microsoft.NETCore.App": "1.0.0-rc2-*"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dnxcore50",
"portable-net45"
]
}
},
"runtimes": {
"win10-x64": {}
}
}
Response
HTTP/1.1 200 OK
Date: Sun, 17 Apr 2016 22:10:45 GMT
Content-Type: application/xml
Server: Kestrel
Content-Length: 75
<?xml version="1.0" encoding="UTF-8"?>
<sample>
Hello World.
</sample>
Here it is on GitHub for good measure. :)
You could do return Content(xmlString, "application/xml")
but that's probably not the best way to do it, unless they are stored in this way on the filesystem or DB.
Usually you would want to have strong typed classes which you return from your actions and have them serialize it as xml.
You can also tell your actions to return the content based on the accept header (i.e. either json or xml) but for xml you need to register the xml serializers first iirc.
services.AddMvc(...)
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
and annotate your actions
[Produces("application/json", "application/xml")]
public Task<IActionResult> Get()
{
User user = ...........;
return ObjectResult(user);
}
If the client sends Accept: application/xml
then it will return xml and if the client sends Accept: application/json
it returns json.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With