【问题标题】:Azure Function How To Return XML Not A StringAzure 函数如何返回 XML 而不是字符串
【发布时间】:2022-12-03 23:00:25
【问题描述】:

我正在尝试从 Azure 函数返回一个 XML 配置文件。配置被写入如下所示的字符串中。

                    xml = @"<note>
                        <to>Tove</to>
                        <from>Jani</from>
                        <heading>Reminder</heading>
                        <body>Don't forget me this weekend!</body>
                        </note>";

然后我像这样归还它

return req.CreateResponse(HttpStatusCode.OK, xml.Replace("\r\n",""));

但是 HTTP 调用的结果是这样的

    "
    <note>
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
</note>"

我如何摆脱封闭的引号?

【问题讨论】:

  • 你试过req.CreateResponse(HttpStatusCode.OK, xml, Configuration.Formatters.XmlFormatter)吗?
  • Configuration.Formatters.XmlFormatter 来自哪个包?我的 VS 说使用 System.Configuration 但它没有 Formatters.XmlFormatter 的定义。

标签: c# xml azure function


【解决方案1】:

你可以使用https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.contentresult?view=aspnetcore-6.0

var xml = @"<note>
                        <to>Tove</to>
                        <from>Jani</from>
                        <heading>Reminder</heading>
                        <body>Don't forget me this weekend!</body>
                        </note>";
 return new ContentResult() { Content = xml, ContentType = "text/xml"};

【讨论】:

  • 如果我尝试我得到 Cannot implicitly convert type 'System.Web.Mvc.ContentResult' to 'System.Net.Http.HttpResponseMessage
  • @JonFarmer 您使用什么目标框架来实现您的功能?您可以在项目设置中检查这一点。
【解决方案2】:

(还不能评论,所以我会和@FreemanRU一起去

但是你的函数返回什么?试试IActionResult

   [FunctionName(nameof(Function1))]
        public Task<IActionResult> Function1(
...

【讨论】:

    猜你喜欢
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2018-11-16
    相关资源
    最近更新 更多