ASP.NET Core: Replacement for Server.MapPath
There’s no Server.MapPath() in ASP.NET Core and things are way different than with old ASP.NET MVC. This blog post shows how application and public web files are organized in ASP.NET Core and how to access them from web applications.
ASP.NET Core offers two different locations for files:
- Content root – this is where application binaries and other private files are held.
- Web root – this is where public files are held (wwwroot folder in web project).
By default web root is located under content root. But there are also deployments where web root is located somewhere else. I have previously seen such deployments on Azure Web Apps. It’s possible that some ISP-s also use different directories in tree for application files and web root.
Getting content and web root at code
Paths to content root and web root are available through IHostingEnvironment in code like shown here.
Notice how content root and wwwroot are located in totally different places in machine.
Setting web root location
To set location for web root we need hosting.json file in application root folder. Also we need some code to include the file – at least for Kestrel. My hosting.json is shown here.
It is loaded when program starts (Program.cs file). I made this file optional so my application doesn’t crash when hosting file is missing.
public class Program
public static void Main(string args)
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true)
public static IWebHostBuilder CreateWebHostBuilder(string args) =>
If there is no hosting file then default configuration is used and ASP.NET Core expects that web root is located under application content root.
Although we don’t have Server.MapPath() call anymore in ASP.NET we have IHostingEnvironment that provides us with paths to application content root and web root. These are full paths to mentioned locations and not URL-s. We can use these paths to read files from both of locations if needed.