如何在 Server.MapPath
我尝试使用 IHostingEnvironment
,但它没有给出正确的结果。
IHostingEnvironment env = new HostingEnvironment();
var str1 = env.ContentRootPath; // Null
var str2 = env.WebRootPath; // Null, both doesn't give any result
我在 wwwroot 文件夹中有一个图像文件(Sample.PNG)我需要获得这个绝对路径。
答案 0 :(得分:70)
将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();
}
}
您可以更进一步,创建自己的路径提供程序服务抽象和实现。
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;
}
}
将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();
}
}
确保使用DI容器注册服务
services.AddSingleton<IPathProvider, PathProvider>();
答案 1 :(得分:4)
*入侵*
不推荐,但仅供参考,您可以使用
var abs = Path.GetFullPath("~/Content/Images/Sample.PNG").Replace("~\\","");
首选上述DI / Service方法,但是如果您处于非DI情况(例如,以Activator
实例化的类),则可以使用。
答案 2 :(得分:3)
更好的解决方案是使用x
方法。
IFileProvider.GetFileInfo()
您必须注册 public IActionResult ResizeCat([FromServices] IFileProvider fileProvider)
{
// get absolute path (equivalent to MapPath)
string absolutePath = fileProvider.GetFileInfo("/assets/images/cat.jpg").PhysicalPath;
...
}
like this to be able to access it through DI:
IFileProvider
如您所见,这种逻辑(文件的来源)可能会变得非常复杂,但是如果更改,代码不会中断。
如果您有特殊的逻辑,则可以使用 // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
var physicalProvider = _hostingEnvironment.ContentRootFileProvider;
var embeddedProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly());
var compositeProvider = new CompositeFileProvider(physicalProvider, embeddedProvider);
// choose one provider to use for the app and register it
//services.AddSingleton<IFileProvider>(physicalProvider);
//services.AddSingleton<IFileProvider>(embeddedProvider);
services.AddSingleton<IFileProvider>(compositeProvider);
}
创建自定义IFileProvider
。我遇到一种情况,我想在中间件中加载图像,然后调整其大小或裁剪它。但这是一个Angular项目,因此部署的应用程序的路径有所不同。我编写的中间件从new PhysicalFileProvider(root)
中提取了IFileProvider
,然后我就可以像过去使用startup.cs
一样使用GetFileInfo()
。
答案 3 :(得分:2)
变量1:
string path = System.IO.Directory.GetCurrentDirectory();
Var 2:
string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.IndexOf("\\bin"));
答案 4 :(得分:1)
感谢@NKosi,但是IHostingEnvironment
在MVC core 3中已经过时了!!
根据this:
过时的类型(警告):
Microsoft.Extensions.Hosting.IHostingEnvironment
Microsoft.AspNetCore.Hosting.IHostingEnvironment
Microsoft.Extensions.Hosting.IApplicationLifetime
Microsoft.AspNetCore.Hosting.IApplicationLifetime
Microsoft.Extensions.Hosting.EnvironmentName
Microsoft.AspNetCore.Hosting.EnvironmentName
新类型:
Microsoft.Extensions.Hosting.IHostEnvironment
Microsoft.AspNetCore.Hosting.IWebHostEnvironment : IHostEnvironment
Microsoft.Extensions.Hosting.IHostApplicationLifetime
Microsoft.Extensions.Hosting.Environments
因此,您必须使用IWebHostEnvironment
而不是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);
}
}