如何在ASP.Net Core中为Server.MapPath获取绝对路径

Sha*_*_92 29 c# asp.net asp.net-core-mvc .net-core asp.net-core

如何获得ASP网络核心替代方式的绝对路径 Server.MapPath

我试过使用IHostingEnvironment但它没有给出正确的结果.

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

我在wwwroot文件夹中有一个图像文件(Sample.PNG)我需要获取这个绝对路径.

Nko*_*osi 77

注入IHostingEnvironment作为依赖,依赖类.该框架将为您填充它

public class HomeController : Controller {
    private IHostingEnvironment _hostingEnvironment;

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

    [HttpGet]
    public IActionResult Get() {
        var path = Path.Combine(_hostingEnvironment.WebRootPath, "Sample.PNG");
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以更进一步,创建自己的路径提供程序服务抽象和实现.

public interface IPathProvider {
    string MapPath(string path);
}

public class PathProvider : IPathProvider {
    private IHostingEnvironment _hostingEnvironment;

    public PathProvider(IHostingEnvironment environment) {
        _hostingEnvironment = environment;
    }

    public string MapPath(string path) {
        var filePath = Path.Combine(_hostingEnvironment.WebRootPath, path);
        return filePath;
    }
}
Run Code Online (Sandbox Code Playgroud)

并注入IPathProvider依赖类.

public class HomeController : Controller {
    private IPathProvider pathProvider;

    public HomeController(IPathProvider pathProvider) {
        this.pathProvider = pathProvider;
    }

    [HttpGet]
    public IActionResult Get() {
        var path = pathProvider.MapPath("Sample.PNG");
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

确保使用DI容器注册服务

services.AddSingleton<IPathProvider, PathProvider>();
Run Code Online (Sandbox Code Playgroud)


Ale*_* S. 8

.NET 核心 3.0

变量 1:

string path = System.IO.Directory.GetCurrentDirectory();
Run Code Online (Sandbox Code Playgroud)

变量 2:

string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.IndexOf("\\bin"));
Run Code Online (Sandbox Code Playgroud)


ami*_*133 6

.Net 核心 3

例如我想定位 ~/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();
    }
}
Run Code Online (Sandbox Code Playgroud)

一些技巧

此外,如果您没有控制器或服务,请按照最后一部分并将其注册为单例。然后,在 Startup.ConfigureServices 中:

services.AddSingleton<your_class_Name>();
Run Code Online (Sandbox Code Playgroud)

最后,your_class_Name在需要的地方注入。


.Net 核心 2

例如我想定位 ~/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();
    }
}
Run Code Online (Sandbox Code Playgroud)

更多细节

感谢@NKosi,但IHostingEnvironment在 MVC 核心 3 中已过时!!

根据这个

过时的类型(警告):

Microsoft.Extensions.Hosting.IHostingEnvironment
Microsoft.AspNetCore.Hosting.IHostingEnvironment
Microsoft.Extensions.Hosting.IApplicationLifetime
Microsoft.AspNetCore.Hosting.IApplicationLifetime
Microsoft.Extensions.Hosting.EnvironmentName
Microsoft.AspNetCore.Hosting.EnvironmentName
Run Code Online (Sandbox Code Playgroud)

新类型:

Microsoft.Extensions.Hosting.IHostEnvironment
Microsoft.AspNetCore.Hosting.IWebHostEnvironment : IHostEnvironment
Microsoft.Extensions.Hosting.IHostApplicationLifetime
Microsoft.Extensions.Hosting.Environments 
Run Code Online (Sandbox Code Playgroud)

所以你必须使用IWebHostEnvironment而不是IHostingEnvironment.


小智 5

*哈克* 不推荐,但仅供参考,您可以使用 var abs = Path.GetFullPath("~/Content/Images/Sample.PNG").Replace("~\\","");

首选上面的DI / Service方法,但是如果您处于非DI情况下(例如,用实例化的类Activator),则可以使用该方法。