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.
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, 2022Comments
-
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 asSystem.Web
). Here is a small example:sb.AppendLine("\"New Path\": \"" + newFile.FullName.Replace(HttpRuntime.AppDomainAppPath, "/");
Any help would be appreciated, thanks
-
Dr. Roggia almost 7 yearsI've already added an IHontingEnvironment element inside the class. So ContentRootPath will give me the same result as HttpRuntime,AppDomainPath?
-
meziantou almost 7 years
ContentRootPath
give the location set inUseContentRoot
extension method inProgram.cs
. By defaultUseContentRoot(Directory.GetCurrentDirectory())
. So, it should give you the same result asHttpRuntime,AppDomainPath
. -
Dr. Roggia almost 7 yearsAnd what if i need the URL Path?
-
Maly Lemire over 6 yearsWhat if I need it in static constructor?
-
meziantou over 6 yearsInitialize a static property in the
Configure
method. Then you can get the value of this property in the static constructor. -
Roboneter over 5 yearsI get my C:/program/../../ result from this, and AppDomainAppPath gives me a path like /LM/W3C/<site id>/<servicename> what am i doing wrong?