如何在 ASP.Net Core 替代方式中获取 Server.MapPath 的绝对路径

How to get absolute path in ASP.Net Core alternative way for Server.MapPath(如何在 ASP.Net Core 替代方式中获取 Server.MapPath 的绝对路径)
本文介绍了如何在 ASP.Net Core 替代方式中获取 Server.MapPath 的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 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 

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

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

推荐答案

从 .Net Core v3.0 开始,应该是 IWebHostEnvironment 访问已移至 Web 特定环境接口的 WebRootPath.

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

IWebHostEnvironment 作为依赖注入到依赖类中.该框架将为您填充它

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

public class HomeController : Controller {
    private IWebHostEnvironment _hostEnvironment;

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

    [HttpGet]
    public IActionResult Get() {
        string path = Path.Combine(_hostEnvironment.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 IWebHostEnvironment _hostEnvironment;

    public PathProvider(IWebHostEnvironment environment) {
        _hostEnvironment = environment;
    }

    public string MapPath(string path) {
        string filePath = Path.Combine(_hostEnvironment.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() {
        string 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 的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)