0

I have a .NET Core 2.0 solution containing 3 projects (DataAccess, Services, WebAPI). In the wwwroot folder of the WebAPI project I've created a templates folder that contains a couple of Excel template files. In Services I have an Export folder which contains the ExportService. The ExportService uses the templates to generate some reports. I have a private method that 'gets' the path to the templates directory. In fact I'm just specifying a relative path to it, which works fine when debugging, but not when I publish the project. How can I make the files available/reachable in my published app?

private string GetExcelTemplatePath()
{
    var templatesDirectory = "..\\WebAPI\\wwwroot\\templates";
    var templateName = "Excel_template_v1.xlsx";
    var templatePath = Path.Combine(templatesDirectory, templateName);

    return templatePath;
}
kanpeki
  • 435
  • 2
  • 6
  • 20

1 Answers1

1

With ASP.NET Core you’ll want to inject IHostingEnvironment into your controller (or wherever you need this). From that object you can get WebRootPath which contains the path to wwwroot. Use that to find the files inside it.

More information from the web.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • Hi, Sami. Is there no way I can do this from Services? Somebody else worked on the front end part and it's a separate project I don't have access to. I don't want to mess up the whole project... – kanpeki Nov 16 '17 at 08:21
  • @kanpeki I think you can inject it in method calls also, or give it as an argument for the service when it’s created. – Sami Kuhmonen Nov 16 '17 at 08:27