What is the equivalent of Server.MapPath in ASP.NET Core?

后端 未结 4 1128
清酒与你
清酒与你 2020-11-28 14:24

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 con

相关标签:
4条回答
  • 2020-11-28 14:32

    .Net Core 3

    For example I want to locate ~/wwwroot/CSS

    public class YourController : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
    
        public YourController (IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment= webHostEnvironment;
        }
    
        public IActionResult Index()
        {
            string webRootPath = _webHostEnvironment.WebRootPath;
            string contentRootPath = _webHostEnvironment.ContentRootPath;
    
            string path ="";
            path = Path.Combine(webRootPath , "CSS");
            //or path = Path.Combine(contentRootPath , "wwwroot" ,"CSS" );
            return View();
        }
    }
    

    Some Tricks

    Also if you don't have a controller or service,follow last Part and register it's class as a singleton. Then, in Startup.ConfigureServices:

    services.AddSingleton<your_class_Name>();
    

    Finally, inject your_class_Name where you need it.


    .Net Core 2

    For example I want to locate ~/wwwroot/CSS

    public class YourController : Controller
    {
        private readonly IHostingEnvironment _HostEnvironment;
    
        public YourController (IHostingEnvironment HostEnvironment)
        {
            _HostEnvironment= HostEnvironment;
        }
    
        public ActionResult Index()
        {
            string webRootPath = _HostEnvironment.WebRootPath;
            string contentRootPath = _HostEnvironment.ContentRootPath;
    
            string path ="";
            path = Path.Combine(webRootPath , "CSS");
            //or path = Path.Combine(contentRootPath , "wwwroot" ,"CSS" );
            return View();
        }
    }
    

    MoreDetails

    Thanks to @Ashin but IHostingEnvironment is obsoleted in MVC 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.

    0 讨论(0)
  • 2020-11-28 14:51

    The accepted answer's suggestion is good enough in most scenarios, however - since it depends on Dependency Injection - is limited to Controllers and Views: in order to have a proper Server.MapPath replacement that can be accessed from non-singleton helper classes we can add the following line(s) of code at the end of the Configure() method of the app's Startup.cs file:

    // setup app's root folders
    AppDomain.CurrentDomain.SetData("ContentRootPath", env.ContentRootPath);
    AppDomain.CurrentDomain.SetData("WebRootPath", env.WebRootPath);
    

    This way we'll be able to retrieve them from within any class (including, yet not limiting to, Controllers and Views) in the following way:

    var contentRootPath = (string)AppDomain.CurrentDomain.GetData("ContentRootPath");
    var webRootPath = string)AppDomain.CurrentDomain.GetData("WebRootPath");
    

    This can be further exploited to create a static helper method that will allow us to have the same functionality as the good old Server.MapPath:

    public static class MyServer 
    {
        public static string MapPath(string path)
        {
            return Path.Combine(
                (string)AppDomain.CurrentDomain.GetData("ContentRootPath"), 
                path);
        }
    }
    

    Which can be used it in the following way:

    var docPath = MyServer.MapPath("App_Data/docs");
    

    For additional info regarding this approach and a bit of background, take a look at this post on my blog.

    0 讨论(0)
  • 2020-11-28 14:52

    use for example: var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");

    0 讨论(0)
  • 2020-11-28 14:53

    UPDATE: IHostingEnvironment is deprecated. See update below.

    In Asp.NET Core 2.2 and below, 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. You should be using IWebHostEnvironment instead of IHostingEnvironment. Please refer to that answer below.

    Microsoft has neatly segregated the host environment properties among these interfaces. Please refer to the interface definition below:

    namespace Microsoft.Extensions.Hosting
    {
      public interface IHostEnvironment
      {
        string EnvironmentName { get; set; }
        string ApplicationName { get; set; }
        string ContentRootPath { get; set; }
        IFileProvider ContentRootFileProvider { get; set; }
      }
    }
    
    namespace Microsoft.AspNetCore.Hosting
    {
      public interface IWebHostEnvironment : IHostEnvironment
      {
        string WebRootPath { get; set; }
        IFileProvider WebRootFileProvider { get; set; }
      }
    }
    
    0 讨论(0)
提交回复
热议问题