Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute path in ASP.Net Core alternative way for Server.MapPath

How to get absolute path in ASP net core alternative way for Server.MapPath

I have tried to use IHostingEnvironment but it doesn't give proper result.

IHostingEnvironment env = new HostingEnvironment(); var str1 = env.ContentRootPath; // Null var str2 = env.WebRootPath; // Null, both doesn't give any result  

I have one image file (Sample.PNG) in wwwroot folder I need to get this absolute path.

like image 960
Shanmu_92 Avatar asked May 16 '17 04:05

Shanmu_92


People also ask

What is Server MapPath in asp net?

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

What is Server MapPath in MVC?

For example, Server. MapPath() allows a path of "files/file1. doc". It uses the current context to determine the path of the current page, for example, and then creates the relative path from there.

How to get the absolute path of the application content files?

The ContentRootPath property will give you access to the absolute path to the application content files. You may also use the property, WebRootPath if you would like to access the web-servable root path (www folder by default) You may inject this dependency into your controller and access it as follows:

Is it possible to do opposite of server mappath in ASP NET?

Unfortunately, ASP.NET doesn't have a function which would do opposite of Server.MapPath. But, task is not so difficult, we have to only remove root folder part and turn slashes to right side. Function getVirtualPath could look like this:

What is the difference between relative path and mappath?

The relative path does not need to specify an existing file or folder for this method to return a value. However, you cannot specify a path outside of the Web application. The MapPath method potentially contains sensitive information about the hosting environment. The return value should not be displayed to users.

How to resolve file paths in ASP NET MVC?

Web application developers who code in classic asp, asp .net, and asp .net MVC have always relied on server.mappath to resolve file paths. But this method is no longer available in asp .net core 5.0 MVC. The is to use IHostEnvironment


2 Answers

As of .Net Core v3.0, it should be IWebHostEnvironment to access the WebRootPath which has been moved to the web specific environment interface.

Inject IWebHostEnvironment as a dependency into the dependent class. The framework will populate it for you

public class HomeController : Controller {     private IWebHostEnvironment _hostEnvironment;      public HomeController(IWebHostEnvironment environment) {         _hostEnvironment = environment;     }      [HttpGet]     public IActionResult Get() {         string path = Path.Combine(_hostEnvironment.WebRootPath, "Sample.PNG");         return View();     } } 

You could go one step further and create your own path provider service abstraction and implementation.

public interface IPathProvider {     string MapPath(string path); }  public class PathProvider : IPathProvider {     private IWebHostEnvironment _hostEnvironment;      public PathProvider(IWebHostEnvironment environment) {         _hostEnvironment = environment;     }      public string MapPath(string path) {         string filePath = Path.Combine(_hostEnvironment.WebRootPath, path);         return filePath;     } } 

And inject IPathProvider into dependent classes.

public class HomeController : Controller {     private IPathProvider pathProvider;      public HomeController(IPathProvider pathProvider) {         this.pathProvider = pathProvider;     }      [HttpGet]     public IActionResult Get() {         string path = pathProvider.MapPath("Sample.PNG");         return View();     } } 

Make sure to register the service with the DI container

services.AddSingleton<IPathProvider, PathProvider>(); 
like image 68
Nkosi Avatar answered Sep 28 '22 07:09

Nkosi


.NET Core 3.0

Var 1:

string path = System.IO.Directory.GetCurrentDirectory(); 

Var 2:

string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.IndexOf("\\bin")); 
like image 21
Alexander S. Avatar answered Sep 28 '22 06:09

Alexander S.