【问题标题】:Return "application/xml" instead of "text/plain" ASP.NET Core Web API返回 "application/xml" 而不是 "text/plain" ASP.NET Core Web API
【发布时间】:2016-04-17 21:06:41
【问题描述】:

我有一个 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>";

【问题讨论】:

  • 你使用的是什么版本的 ASP.NET Core?
  • "sdk": { "version": "1.0.0-rc1-update2", "runtime": "coreclr", "architecture": "x64" }
  • ContentResult 也适用于此。
  • 我只是用你的答案试了一下,Visual Studio 不让我做ContentType = "application/xml", 绝对是正确的方向,结合答案,我能够得到它。

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


【解决方案1】:

简答

如果您有一个 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 很好。 :)

【讨论】:

  • [HttpGet, Produces("application/xml")] public ContentResult Get() { return Content(xmlString, "application/xml"); }(很抱歉格式不好)
【解决方案2】:

您可以使用return Content(xmlString, "application/xml"),但这可能不是最好的方法,除非它们以这种方式存储在文件系统或数据库中。

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

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

services.AddMvc(...)
        .AddXmlSerializerFormatters()
        .AddXmlDataContractSerializerFormatters();

并注释您的操作

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

    return ObjectResult(user);
}

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

【讨论】:

  • “通常你会希望有一个强类型的类,你会从你的操作中返回并让它们将它序列化为 xml。” - 是的,这可行,但这个问题的上下文与this post 有关。我需要附加比对象中包含的更多的数据。
  • 第一行就是做我需要做的事情。谢谢!
  • 当谈到 .NET Core 时,有很多过时的答案和教程,这使得获取答案变得非常耗时。然而,对于最新的 ASP.NET Core (1.1),这个答案是正确的。
  • Produces注解需要使用哪个?
  • @JedatKinports:单击 Ctrl+Dot... 学习使用 Microsoft 提供的工具;)或者查阅文档:docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2012-05-19
  • 2013-08-18
  • 2012-06-02
  • 2020-09-30
  • 2016-03-10
  • 1970-01-01
相关资源
最近更新 更多