We use EPPlus to build and export Excel files. In this scenario, we have a complex template that would be easier to fill in than build from scratch. Assuming we have two folders at the root of the .NET Core 6.0 project, "/Controllers" and "/Templates", I'd like to be able to reference the template from a controller in the "Controllers" folder using a relative path such as "../Templates/template.xlsx".
CodePudding user response:
you can get the path with IWebHostEnvironment. Inject it into your controller.. and..
string path = Path.Combine(_webHostEnvironment.ContentRootPath, "Templates");
string filename = "template.xlsx";
string fullfile = Path.Combine(path, filename);
be sure to check if the directory exists
CodePudding user response:
You can do something like this from your controller. and it will reference the file wherever you deploy code. Not a relative path but still usable in controller code.
string path = Path.Combine(Environment.CurrentDirectory, @"Templates\template.xlsx");
Of course make sure directory and file exist, but if its in code they it should.