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
Please enter at least three characters to search
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • DGS GraphQL and Spring Boot
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Ethical AI in Agile
  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.2K 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
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!