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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
  • GDPR Compliance With .NET: Securing Data the Right Way
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Trending

  • The Death of REST? Why gRPC and GraphQL Are Taking Over
  • Squid Game: The Clean Code Trials — A Java Developer's Survival Story
  • Microservice Madness: Debunking Myths and Exposing Pitfalls
  • The Evolution of Software Integration: How MCP Is Reshaping AI Development Beyond Traditional APIs
  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.

By 
Gunnar Peipman user avatar
Gunnar Peipman
·
Nov. 13, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
34.4K 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.

Related

  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
  • GDPR Compliance With .NET: Securing Data the Right Way
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: