Net Core IWebHostEnvironment getting only "c:\"

Viewed 130

I'm trying to move an app from .net to net core 5, and I'm trying to get the content root.

All of the instructions that I see show using IHostingEnvironment in a controller:

However, using this just get's me "c:" and I suspect that it's because I'm trying to use it, not within a Controller, but within the Identity RegisterModel : PageModel in order to grab an html file that will be a source for the confirmation email.

Could that be the problem?

If so, how would I get the path of a file in code that is not in a controller?

1 Answers

IHostingEnvironment works like a service and needs to be injected in the constructor of the controller. You can use it in your User Controller.

Can't do much considerations without the code to evaluate.

A simple way is: In the constructor of the default controller, set in a static class a member that holds the path for you. Once it's a static class, you can access it from everywhere if you set it to public.

Related