Dr. Roggia
Dr. Roggia

Reputation: 1125

HttpRuntime.AppDomainAppPath equivalent in ASP.NET Core

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

Upvotes: 12

Views: 16233

Answers (1)

meziantou
meziantou

Reputation: 21337

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.

Upvotes: 10

Related Questions