3

I need to use Server.MapPath. Since library projects does not have Startup.cs i cannot apply the normal way.

heimzza
  • 146
  • 8
  • .NET 5 is .NET *Core* 5. The question is unclear, but if you google for `Server.MapPath ASP.NET Core` you'll find a lot of answers. – Panagiotis Kanavos Jun 10 '21 at 12:02
  • 1
    Does this answer your question? [What is the equivalent of Server.MapPath in ASP.NET Core?](https://stackoverflow.com/questions/49398965/what-is-the-equivalent-of-server-mappath-in-asp-net-core) – Panagiotis Kanavos Jun 10 '21 at 12:02
  • So your real question should be: _How do I get a reference to IWebHostEnvironment inside a library project? (and inside a static class)_ – Steve Jun 10 '21 at 12:05
  • Yes, I will submit as you said – heimzza Jun 10 '21 at 12:37
  • This might be helpful and resolve your puzzle. [LINK](https://stackoverflow.com/questions/64482399/how-to-use-iwebhostenvironment-inside-static-class-in-asp-core). Here a static class which have property as `IWebHostEnvironment`. You can call initialize from startup.cs and then use it across your application. – Pashyant Srivastava Jun 10 '21 at 16:52
  • Thanks @PashyantSrivastava. That works for me. – heimzza Jun 11 '21 at 11:14
  • [Link]https://stackoverflow.com/questions/64482399/how-to-use-iwebhostenvironment-inside-static-class-in-asp-core This is the answer – heimzza Jun 11 '21 at 11:16

1 Answers1

2

First, register HttpcontextAccessor service in Startup.cs,

services.AddHttpContextAccessor();

then in the class,

private static HttpContext _httpContext => new HttpContextAccessor().HttpContext;
private static IWebHostEnvironment _env => (IWebHostEnvironment)_httpContext.RequestServices.GetService(typeof(IWebHostEnvironment));

now you can access it in a static class and a static method.

This did the trick for me. If anyone needs.

heimzza
  • 146
  • 8