Return "application / xml" instead of "text / plain" ASP.NET Core Web API

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 text/plain . The content is displayed, but I need the application/xml content type. I have included the RespectBrowserAcceptHeader parameter, which will serialize the objects as XML and set the correct content type, except that 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>"; 
+7
xml asp.net-web-api asp.net-core
source share
2 answers

You can do return Content(xmlString, "application/xml") , but this is probably not the best way to do this if they are not stored this way in the file system or database.

Usually you need to have strong typed classes that you return from your actions and serialize them as xml.

You can also tell your actions about returning content based on the accept header (e.g. json or xml), but for xml you need to register the iirc XML serializers first.

 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 will return json.

+9
source share

Short answer

If you have a string that is XML and needs to return it as an XML document, return a ContentResult .

 [HttpGet] public ContentResult Get() { return new ContentResult { ContentType = "application/xml", Content = xmlString, StatusCode = 200 }; } 

Full example

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 }; } } } 

Enter

 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": {} } } 

answer

 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> 

This is on GitHub for good measure. :)

+12
source share

All Articles