返回" application / xml"而不是" text / plain" ASP.NET Core Web API

时间:2016-04-17 21:06:41

标签: xml asp.net-web-api asp.net-core

我有一个XML字符串,我需要将其作为XML文档返回。默认情况下,返回的内容类型为text/plain。内容已呈现,但我需要内容类型为application/xml。我已经启用了RespectBrowserAcceptHeader选项,它将对象序列化为XML并设置正确的内容类型,除非对象是字符串。

[HttpGet]
public string Get()
{
   return xmlString;
}

public static string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                     <sample>
                         Hello World.
                     </sample>";

2 个答案:

答案 0 :(得分:16)

简答

如果您的字符串是XML并且需要将其作为XML文档返回,则返回ContentResult

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

完整示例

<强>控制器

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

<强>启动

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

<强>响应

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以获得良好的衡量标准。 :)

答案 1 :(得分:13)

你可以做services.AddMvc(...) .AddXmlSerializerFormatters() .AddXmlDataContractSerializerFormatters(); 但这可能不是最好的方法,除非它们以这种方式存储在文件系统或数据库上。

通常,您希望拥有从您的操作返回的强类型类,并将它们序列化为xml。

您还可以根据accept标头(即json或xml)告诉您的操作返回内容,但对于xml,您需要先注册xml序列化程序iirc。

Produces("application/json", "application/xml")]
public Task<IActionResult> Get()
{
    User user = ...;

    return ObjectResult(user);
}

并注释您的行为

Accept: application/xml

如果客户端发送Accept: application/json,则它将返回xml,如果客户端发送windowDrawsSystemBarBackgrounds,则返回json。