Search code examples
xmlasp.net-corestatic-files

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


I have several XML strings which are used as API queries. I would like to store them in a static file in my .NET Core webapi project. Trying to store them in the appsettings.json with commas as seperators or with '+', between lines caused the "Could not parse the JSON file" error, in the startup of the app. Where can I store the XML string ? I would like them to be in a static files so I can control and see them also after deploy. Here is an example for my XML string:

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

Solution

  • If you store it as a xml file directly under a folder within your Web API project, to access it from controller action, you can try:

    Inject IWebHostEnvironment into the constructor of your controller

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

    In controller action

    public IActionResult Get()
    {
        var filepath = Path.Combine(_env.ContentRootPath, @"XmlFiles\" + "test.xml");
    
        //code logic here
    
        return Ok();
    }
    

    The structure of project may look like this

    enter image description here