Howto get the ASP.NET Core ContentRoot path?


There are 3 ways that I have found to be working within Startup.cs

  1. use Environment variables.  This however, only works if you also have added the environmentvariables. (builder.AddEnvironmentVariables(); )
  2. var env = Configuration.GetSection("ASPNETCORE_CONTENTROOT");
    var path =     env.Value

  3. Resolve IHostingEnvironment from Services.
      var sv = (IHostingEnvironment)services.FirstOrDefault(f => f.ServiceType == typeof(IHostingEnvironment)).ImplementationInstance;
    var path = sv.ContentRootPath;
    Though, I can imagine you would rather resolve HostingEnvironment but this does not work during ConfigureServices scope..
  4. Use Directory
    eg:  var path = Directory.GetCurrentDirectory();
blog comments powered by Disqus