DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET Core: Replacement for Server.MapPath

ASP.NET Core: Replacement for Server.MapPath

his blog post shows how application and public web files are organized in ASP.NET Core and how to access them from web applications.

Gunnar Peipman user avatar by
Gunnar Peipman
·
Nov. 13, 18 · Tutorial
Like (2)
Save
Tweet
Share
29.33K Views

Join the DZone community and get the full member experience.

Join For Free

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 ISPs also use different directories in trees for application files and web root.

Getting Content and Web Root in Code

Paths to content root and web root are available through IHostingEnvironment in code, as shown here.

Notice how content root and wwwroot are located in totally different places in the machine.

Setting Web Root's Location

To set a location for web root we need the hosting.json file in the application root folder. Also, we need some code to include in the file — at least for Kestrel. My hosting.json is shown here.

{
  "webRoot": "c:\\temp\\wwwroot\\"
}

It is loaded when the program starts (Program.cs file). I made this file optional so my application doesn't crash when the hosting file is missing.

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
                         .AddJsonFile("hosting.json", optional: true)
                         .Build();

        CreateWebHostBuilder(args).UseConfiguration(config)
                                  .UseKestrel()
                                  .Build()
                                  .Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

If there is no hosting file, then the default configuration is used and ASP.NET Core expects that web root is located under the application's content root.

Wrapping Up

Although we don't have a Server.MapPath()call anymore in ASP.NET, we have IHostingEnvironment which provides us with paths to the application content root and web root. These are full paths to the mentioned locations and not URLs. We can use these paths to read files from both of locations if needed.

ASP.NET ASP.NET Core application

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Promises, Thenables, and Lazy-Evaluation: What, Why, How
  • The Data Leakage Nightmare in AI
  • Top Authentication Trends to Watch Out for in 2023
  • SAST: How Code Analysis Tools Look for Security Flaws

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: