如何以ASP.Net Core替代方式获取Server.MapPath的绝对路径 [英] How to get absolute path in ASP.Net Core alternative way for Server.MapPath

查看:1296
本文介绍了如何以ASP.Net Core替代方式获取Server.MapPath的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP网络核心替代方法中获取 Server.MapPath

How to get absolute path in ASP net core alternative way for Server.MapPath

我尝试使用 IHostingEnvironment ,但是它无法提供正确的结果。

I have tried to use IHostingEnvironment but it doesn't give proper result.

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

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

I have one image file (Sample.PNG) in wwwroot folder I need to get this absolute path.

推荐答案

更新



从.Net Core v3.0开始,它应该是 IWebHostEnvironment 而不是 IHostingEnvironment 作为 WebRootPath 已移至特定于Web的环境界面。

Update

As of .Net Core v3.0, it should be IWebHostEnvironment instead of IHostingEnvironment as the WebRootPath has been moved to the web specific environment interface.

public class HomeController : Controller {
    private IWebHostEnvironment _hostingEnvironment;

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

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



原始答案



IHostingEnvironment 作为依赖项注入到依赖类中。该框架将为您填充

Original Answer

Inject IHostingEnvironment as a dependency into the dependent class. The framework will populate it for you

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

您可以再走一步,创造自己的道路

You could go one step further and create your own path provider service abstraction and implementation.

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

And inject IPathProvider into dependent classes.

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容器中注册该服务

Make sure to register the service with the DI container

services.AddSingleton<IPathProvider, PathProvider>();

这篇关于如何以ASP.Net Core替代方式获取Server.MapPath的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆