.net core webapi项目中xml静态字符串存放位置

Where to store xml static string in .net core webapi project

我有几个 XML 字符串用作 API 查询。 我想将它们存储在我的 .NET Core webapi 项目中的静态文件中。 尝试将它们存储在 appsettings.json 中,使用逗号作为分隔符或使用 '+',在行之间导致 "Could not parse the JSON file" 错误,在应用程序启动时。 我在哪里可以存储 XML 字符串?我希望它们位于静态文件中,这样我也可以在部署后控制和查看它们。 这是我的 XML 字符串的示例:

<fetch mapping="logical" distinct="true">
  <entity name="xxx">
    <attribute name="yyyy" />
  </entity>
</fetch>

如果您将其作为 xml 文件直接存储在 Web API 项目的文件夹下,要从控制器操作访问它,您可以尝试:

IWebHostEnvironment 注入控制器的构造函数

private IWebHostEnvironment _env;

public ValuesController(IWebHostEnvironment env)
{
    _env = env;
}

在控制器动作中

public IActionResult Get()
{
    var filepath = Path.Combine(_env.ContentRootPath, @"XmlFiles\" + "test.xml");

    //code logic here

    return Ok();
}

项目的结构可能是这样的