HttpRuntime.AppDomainAppPath equivalent in ASP.NET Core

10,313

The IWebHostEnvironment interface provides information about the environment including the base path (ContentRootPath). You can get an instance using dependency injection.

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // env.ContentRootPath;
    }
}

EDIT: Previous version of this answer was using now obsolete IHostingEnvironment. Consider using it with .net core version 2.2 or earlier. Credit for pointing this out goes to @Jack Miller.

Share:
10,313
Dr. Roggia
Author by

Dr. Roggia

I work as a web programmer since 2016, using for the most javascript, c# and ASP.NET/Core Contact me: [email protected]

Updated on June 26, 2022

Comments

  • Dr. Roggia
    Dr. Roggia almost 2 years

    What is the equivalent of HttpRuntime.AppDomainAppPath in .NET Core? I moved a project from ASP.NET to core, and a few Libraries are not included (Such as System.Web). Here is a small example:

    sb.AppendLine("\"New Path\": \"" + newFile.FullName.Replace(HttpRuntime.AppDomainAppPath, "/");
    

    Any help would be appreciated, thanks

  • Dr. Roggia
    Dr. Roggia almost 7 years
    I've already added an IHontingEnvironment element inside the class. So ContentRootPath will give me the same result as HttpRuntime,AppDomainPath?
  • meziantou
    meziantou almost 7 years
    ContentRootPath give the location set in UseContentRoot extension method in Program.cs. By default UseContentRoot(Directory.GetCurrentDirectory()). So, it should give you the same result as HttpRuntime,AppDomainPath.
  • Dr. Roggia
    Dr. Roggia almost 7 years
    And what if i need the URL Path?
  • Maly Lemire
    Maly Lemire over 6 years
    What if I need it in static constructor?
  • meziantou
    meziantou over 6 years
    Initialize a static property in the Configure method. Then you can get the value of this property in the static constructor.
  • Roboneter
    Roboneter over 5 years
    I get my C:/program/../../ result from this, and AppDomainAppPath gives me a path like /LM/W3C/<site id>/<servicename> what am i doing wrong?