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

编程入门 行业动态 更新时间:2024-10-18 01:34:17
本文介绍了如何以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的绝对路径

本文发布于:2023-11-15 23:33:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绝对路径   方式   Net   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!