By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .

Background

In traditional asp.net applications, Server.MapPath is commonly used to generate absolute path in the web server. However, this has been removed from ASP.NET Core. So what is the equivalent way of doing it?

IHostingEnvironment

In ASP.NET Core, all the severable resources are located in wwwroot folder and the whole site is actually an ‘Console’ application.  To access that folder, we can use IHostingEnvironment.WebRootPath property.

Sample Code

IHostingEnvironment is injected and can be consumed in any controller or services.

private readonly IHostingEnvironment environment;
        public MetaWeblogXmlRpcService(
             IHostingEnvironment environment)
         {
             this.environment = environment;
         }

And then in your code, you can reference

var blogImageFolder = Path.Combine(environment.WebRootPath, imageFolder, blog.UniqueName);

The path information can then be used to manipulate I/O operations and etc..

How to Generate Absolute Url in non-MVC context

In your middleware or other scenarios, you may not have access to UrlHelper as you can do in MVC controller or views. Under this situation, you can still build absolute URL with HttpContext.

* In Middleware, you can inject HttpContext instance.

                 var imageVirtualPath = Path.Combine(imageFolder, blog.UniqueName, fileName);
                 var request = HttpContext.Request;
                 var uriBuilder = new UriBuilder
                 {
                     Host = request.Host.Host,
                     Scheme = request.Scheme,
                     Path = imageVirtualPath
                 };
                if (request.Host.Port.HasValue)
                     uriBuilder.Port = request.Host.Port.Value;
                var url = uriBuilder.ToString();
info Last modified by Raymond at 2 months ago * This page is subject to Site terms.

More from Kontext

local_offer asp.net core 2 local_offer asp.net core local_offer dotnetcore local_offer open-banking

visibility 215
thumb_up 0
access_time 2 years ago

I’ve just started an asp.net core 2.2 based implementation for Australia Consumer Data Standards (published by Data 61). Opening Banking initiative will follow these standards. The purpose is to help you to get familiar with these standards, especially the APIs that need to be implemented. ...

open_in_new View

local_offer asp.net core 2 local_offer .net core

visibility 1845
thumb_up 0
access_time 2 years ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

open_in_new View

local_offer asp.net core 2

visibility 2497
thumb_up 0
access_time 3 years ago

Context When I followed OpenIDDict refresh flow sample, I constantly got the issue “The refresh token is no longer valid”, which is returned by the following code in my authorization web api controller: result.Content = new OpenIdConnectResponse  &...

open_in_new View

local_offer asp.net core 2 local_offer asp.net core

visibility 1084
thumb_up 0
access_time 3 years ago

Other related issues are found during my migration. Unable to Change Identity Table Names ...

open_in_new View

info About author

comment Comments (2)

comment Add comment

Please log in or register to comment. account_circle Log in person_add Register
R
Raymondarrow_drop_down

I'm glad it helped. :) It took me a while when I migrated my website to .NET Core a few years ago.


format_quote

person TaterHead access_time 11 months ago
Re: Server.MapPath Equivalent in ASP.NET Core 2

Thanks... perfect!  New to Core (2.2) ... I was wondering where Server.MapPath() went!

reply Reply
account_circle TaterHead

Thanks... perfect!  New to Core (2.2) ... I was wondering where Server.MapPath() went!


reply Reply
Kontext Column

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.

Learn more arrow_forward
info Follow us on Twitter to get the latest article updates. Follow us