0

I´m trying to output a xml file that I have in my .net core api project and then accessing that file later on in the code.

I have this code in my project.json to output the xml

"buildOptions": {
"emitEntryPoint": true,
    "copyToOutput":  "myfile.xml"
"preserveCompilationContext": true
}

And then I try to access it like this

    public Startup(IHostingEnvironment env)
    {
        var path = env.ContentRootPath;
        var filepath = Path.Combine(path, "myfile.xml");
        var doc = new XmlDocument();
        doc.Load(fileName); //throws error
    }

But when I run this in Azure I get this error

FileNotFoundException - Could not find file 'D:\home\site\wwwroot\myfile.xml'.

I have tried every version of e.g this answer but with no luck.

UPDATE 1

Here is my Main method in Program.cs

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

I tried to find the xml file by browsing (with Kudu Service) every thinkable folder so I´m thinking that the xml file doesn´t get build out https://myvsts.scm.azurewebsites.net/api/vfs/site/wwwroot/

UPDATE 2

I found out that copyToOutput works just fine locally but not on azure. I implemented this to browse the folder and my xml file does not show up until I right click it and press publish! Its like the project publish doesn´t pick it up...

Community
  • 1
  • 1
Sturla
  • 3,446
  • 3
  • 39
  • 56

1 Answers1

0

It appears your ContentRootPath is set to the wwwroot folder. Normally, this is is set to the folder that contains wwwroot and IHostingEnvironment.WebRootPath points to the "wwwroot" path.

Check that your Program.Main class has this:

var host = new WebHostBuilder()
   .UseContentRoot(Directory.GetCurrentDirectory())

Also, you may need to also add this file to "publishOptions". https://learn.microsoft.com/en-us/dotnet/articles/core/tools/project-json#publishoptions

"publishOptions": {
  "include": ["myfile.xml"]
}
natemcmaster
  • 25,673
  • 6
  • 78
  • 100
  • I´m not quite following you :-) I updated my question. Could it be that the xml is not buiding out? – Sturla Feb 16 '17 at 11:25
  • Ok I spotted "contains", I misser that. But regardless I can't find the file while browsing. I also tried to add the file to another class library and set it to copyIfNewer but still can't find the file... – Sturla Feb 16 '17 at 11:34
  • Hmm. I added one more suggestion, although it appears from updates that you figured it out. – natemcmaster Feb 16 '17 at 16:07