Server.MapPath Equivalent in ASP.NET Core 2

access_time 4 years ago visibility18498 comment 2

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 12 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 9177
thumb_up 0
access_time 4 years ago

After installed Visual Studio 2017 15.3 preview and .net core 2.0 preview SDK, I upgraded one of my existing asp.net core project to 2.0. The project was created using ‘dotnet new angular’ SPA template.  I also upgraded all the client app packages to the latest. For example, Angular is ...

visibility 3045
thumb_up 0
access_time 4 years ago

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 ...

visibility 1185
thumb_up 0
access_time 4 years ago

Other related issues are found during my migration. https://stackoverflow.com/questions/46118930/unable-to-change-asp-identity-table-names-asp-net-core-2 I faced the same issue as the above post. To fix it, I need to derive my database context with all the parameters specified: public ...