【问题标题】:Produces data annotation not working for xml in asp.net core 2.1在 asp.net core 2.1 中生成不适用于 xml 的数据注释
【发布时间】:2019-08-01 11:34:16
【问题描述】:

我正在使用[Produces("application/xml")] 数据注释在XML 中返回我的响应,但不幸的是它没有返回任何东西。当我删除 [Produces] 数据注释时,它会返回 JSON 格式的数据。我还添加了AddXmlSerializerFormatters() 格式化程序。

这是我的控制器操作

[HttpGet("Generate")]
[Produces("application/xml")]
public XDocument Get()
{
    XDocument sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
             new XElement("urlset", XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9"),
                  from item in business
                  select CreateItemElement(item)
                  )
             );

        return Ok(sitemap.ToString());
}

这是我在启动类中的 ConfigureService 方法

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc().AddXmlSerializerFormatters()
     .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

     services.AddDbContext<ListingDbContext>
            (options => options.UseSqlServer(Configuration.GetConnectionString("App4Rental_Website_DB")));
      services.AddTransient<IRentalRepository, RentalRepository>();
      services.AddTransient<IScrapingRepository, ScrapingRepository>();
}

它的 JSON 结果工作正常,但不适用于 XML。我无法理解问题。

【问题讨论】:

  • 你有没有看过here也许它可以提供一些帮助
  • @lzzy 是的,我也关注了这个。但还是不行。
  • 您要发送哪个接受标头? aspnet 将返回请求的格式,因此您需要请求 XML 获取 XML
  • 客户端必须明确请求内容格式。见Content Negotiation
  • 使用XNamespace.Get("http://www.sitemaps.org/schemas/sitemap/0.9") + "urlset"添加命名空间。

标签: c# xml asp.net-core


【解决方案1】:

对于XDocument,不应该序列化为xml格式。

一般来说,我们使用 xml 格式化程序返回像 Product 这样的对象。你可以尝试返回Product 来测试[Produces("application/xml")]

如果你想返回XDocument,你可以考虑像这样直接返回字符串

public string Get()
{
    XDocument srcTree = new XDocument(
        new XComment("This is a comment"),
        new XElement("Root",
            new XElement("Child1", "data1"),
            new XElement("Child2", "data2"),
            new XElement("Child3", "data3"),
            new XElement("Child2", "data4"),
            new XElement("Info5", "info5"),
            new XElement("Info6", "info6"),
            new XElement("Info7", "info7"),
            new XElement("Info8", "info8")
        )
    );

    XDocument doc = new XDocument(
        new XComment("This is a comment"),
        new XElement("Root",
            from el in srcTree.Element("Root").Elements()
            where ((string)el).StartsWith("data")
            select el
        )
    );
    return doc.ToString();
}

更新:

预期结果是由错误的 XDocument 创建引起的。试试下面的方法:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XDocument sitemap = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
    new XElement(ns + "urlset",
        new XElement(ns + "url",
            new XElement(ns + "loc", "http://app4rental.com/business/100/r.s.-enterprises"),
            new XElement(ns + "lastmod", "2019-08-01"),
            new XElement(ns + "changefreq", "weekly"),
            new XElement(ns + "priority", "0.8")
    )));

return sitemap.ToString();

【讨论】:

  • 是的,它对我有用,但返回的 XML 格式不正确。我已经更新了我的控制器代码,请查看。
  • 非常感谢。根据我的问题,您的回答解决了我的问题。现在我将解决 XML 格式问题。
猜你喜欢
  • 2019-02-11
  • 2019-05-09
  • 2020-03-23
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多