Make .xlsx template file available in published .NET Core 2.0 app

250 views Asked by At

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;
}
1

There are 1 answers

2
Sami Kuhmonen On

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.