What is the equivalent of Server.MapPath in ASP.NE

2020-01-28 09:40发布

问题:

I have this line in some code I want to copy into my controller, but the compiler complains that

The name 'Server' does not exist in the current context

var UploadPath = Server.MapPath("~/App_Data/uploads")

How can I achieve the equivalent in ASP.NET Core?

回答1:

In Asp.NET Core, the hosting environment has been abstracted using the interface, IHostingEnvironment

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:

public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;

            return Content(webRootPath + "\n" + contentRootPath);
        }
    }

UPDATE

IHostingEnvironment has been marked obsolete with .NET Core 3.0 as pointed out by @amir133. Please use IWebHostEnvironment as shown below if the target framework is .NET Core 3.0:

public class HomeController : Controller
    {
        private readonly IWebHostEnvironment _hostingEnvironment;

        public HomeController(IWebHostEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;

            return Content(webRootPath + "\n" + contentRootPath);
        }
    }


回答2:

Thanks to @ashin for his answer but IHostingEnvironment is obsoleted in .Net core 3!!

according to this :

Obsolete types (warning):

Microsoft.Extensions.Hosting.IHostingEnvironment
Microsoft.AspNetCore.Hosting.IHostingEnvironment
Microsoft.Extensions.Hosting.IApplicationLifetime
Microsoft.AspNetCore.Hosting.IApplicationLifetime
Microsoft.Extensions.Hosting.EnvironmentName
Microsoft.AspNetCore.Hosting.EnvironmentName

New types:

Microsoft.Extensions.Hosting.IHostEnvironment
Microsoft.AspNetCore.Hosting.IWebHostEnvironment : IHostEnvironment
Microsoft.Extensions.Hosting.IHostApplicationLifetime
Microsoft.Extensions.Hosting.Environments 

So you must use IWebHostEnvironment instead of IHostingEnvironment.

public class HomeController : Controller
{
    private readonly IWebHostEnvironment _webHostEnvironment;

    public HomeController(IWebHostEnvironment webHostEnvironment)
    {
        _webHostEnvironment= webHostEnvironment;
    }

    public IActionResult Index()
    {
        string webRootPath = _webHostEnvironment.WebRootPath;
        string contentRootPath = _webHostEnvironment.ContentRootPath;

        return Content(webRootPath + "\n" + contentRootPath);
    }
}