Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 752
Dr. Roggia Avatar asked May 11 '17 13:05

Dr. Roggia


1 Answers

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.

like image 166
meziantou Avatar answered Sep 18 '22 14:09

meziantou