打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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


18
1 年前

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

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

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

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

3回答

  • 1
    8 个月前

    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();    }}

    确保使用容器注册服务

    services.AddSingleton<IPathProvider, PathProvider>();

  • 52
    8 个月前

    更好的解决方案是使用 IFileProvider.GetFileInfo() 方法 .

    public IActionResult ResizeCat([FromServices] IFileProvider fileProvider)    {        // get absolute path (equivalent to MapPath)        string absolutePath = fileProvider.GetFileInfo("/assets/images/cat.jpg").PhysicalPath;          ...     }

    你必须注册 IFileProvider like this to be able to access it through DI

    // 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);    }

    正如您所看到的那样,逻辑(文件来自哪里)可能变得非常复杂,但如果更改,您的代码将不会中断 .

    如果您有一些特殊逻辑,可以使用 new PhysicalFileProvider(root) 创建自定义 IFileProvider . 我有一种情况,我想在中间件中加载图像,并调整大小或裁剪它 . 但它是一个Angular项目,因此部署的应用程序的路径不同 . 我编写的中间件从 startup.cs 获取 IFileProvider 然后我可以使用 GetFileInfo() ,就像我过去使用 MapPath 一样 .


  • 0
    8 个月前

    *** Hack *** 不推荐,但是你可以通过 var abs = Path.GetFullPath("~/Content/Images/Sample.PNG").Replace("~\\",""); 获得相对路径的绝对路径

    更喜欢上面的DI /服务方法,但如果您处于非DI情况(例如,用 Activator 实例化的类),这将起作用 .

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ASP.NET Core File Providers
【无私分享:ASP.NET CORE 项目实战(第七章)】文件操作 FileHelper
两点一斜杠和一点一斜杠及一斜杠是什么意思?
ASP.NET Core MVC 模块化
教你如何暴库(网站入侵)
判断文件是否存在
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服