17

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>";
7
  • 1
    What version of ASP.NET Core are you using? Commented Apr 17, 2016 at 22:16
  • 1
    "sdk": { "version": "1.0.0-rc1-update2", "runtime": "coreclr", "architecture": "x64" } Commented Apr 18, 2016 at 0:23
  • 1
    ContentResult will work for that too. Commented Apr 18, 2016 at 0:46
  • 1
    I just tried it with your answer, and Visual Studio wasn't letting me do ContentType = "application/xml", Definitely the right direction, and combining the answers, I was able to get it. Commented Apr 18, 2016 at 0:51
  • 1
    Odd. Object initializers have been around since C# 3.0. Commented Apr 18, 2016 at 0:57

2 Answers 2

31

Short Answer

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

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

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. :)

Sign up to request clarification or add additional context in comments.

2 Comments

[HttpGet, Produces("application/xml")] public ContentResult Get() { return Content(xmlString, "application/xml"); } (sorry for terrible formatting)
@Daric What have you tried to explain ?
25

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.

6 Comments

"Usually you would want to have strong typed classes which you return from your actions and have them serialize it as xml." - Yes, and this works, but the context of this question is in relation to this post. I need to attach more data than what is contained within the object.
The very first line is doing what I needed it to. Thanks!
When it comes to .NET Core there are a LOT of outdated answers and tuturials out there that make it time consuming to get answers. This answer however is CORRECT for the latest ASP.NET Core (1.1).
Which using is needed for Produces annotation?
@JedatKinports: Click Ctrl+Dot... learn to use the tools you were given to from Microsoft ;) Alternatively consult the documentation: learn.microsoft.com/en-us/dotnet/api/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.