This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

Server.MapPath Equivalent in ASP.NET Core 2

5092 views 0 comments last modified about 11 months ago Raymond

asp.net core 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();

Related pages

ASP.NET Core 2.1 Error - 'Cyrillic' is not a supported encoding name

86 views   0 comments last modified about 2 months ago

After upgrading to ASP.NET Core 2.1 (.NET Core SDK 2.1.301), you may encounter the following error about encoding: System.ArgumentException    HResult=0x80070057    Message='Cyrillic' is not a supported encoding name. For information on defining a custo...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

25 views   0 comments last modified about 2 months 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...

View detail

ASP.NET Core 2 with Bootstrap 4 Bundler Minifier Issue: Expected semicolon or closing curly-brace found '-'

783 views   1 comments last modified about 8 months ago

Background When upgrading Bootstrap to v4.0.0 release, the bundler and minifier doesn’t work properly due to CSS variable is commonly used: :root{--blue:#007bff; About 26 errors will show up in the Error List with the following message: ...

View detail

Issue - Unable to get property 'apply' of undefined or null reference occurred in Angular 4.*, VS2017 15.3, ASP.NET Core 2.0

6853 views   10 comments last modified about 2 years ago

Issue Context 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 exa...

View detail

Retrieve Http client request metadata like IP address and languages in asp.net core

1217 views   0 comments last modified about 11 months ago

IP Address In ASP.NET Core, Request.UserHostAddress has been removed though that attribute exists in the traditional ASP.NET applications. We can use HttpContext.Connection to retrieve the remove client IP address: var ipAddress = HttpContext.Connecti...

View detail

Retrieve Identity username, email and other information in ASP.NET Core

1432 views   0 comments last modified about 11 months ago

The identity system in ASP.NET has evolved over time. If you are using ASP.NET Core, you probably found User property is an instance of ClaimsPrincipal in Controller or Razor views. Thus to retrieve the information, you need to utilize the claims.

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.