• <bdo id='bKtxA'></bdo><ul id='bKtxA'></ul>
    <legend id='bKtxA'><style id='bKtxA'><dir id='bKtxA'><q id='bKtxA'></q></dir></style></legend>

  • <tfoot id='bKtxA'></tfoot>

    <small id='bKtxA'></small><noframes id='bKtxA'>

    <i id='bKtxA'><tr id='bKtxA'><dt id='bKtxA'><q id='bKtxA'><span id='bKtxA'><b id='bKtxA'><form id='bKtxA'><ins id='bKtxA'></ins><ul id='bKtxA'></ul><sub id='bKtxA'></sub></form><legend id='bKtxA'></legend><bdo id='bKtxA'><pre id='bKtxA'><center id='bKtxA'></center></pre></bdo></b><th id='bKtxA'></th></span></q></dt></tr></i><div id='bKtxA'><tfoot id='bKtxA'></tfoot><dl id='bKtxA'><fieldset id='bKtxA'></fieldset></dl></div>

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

        How to get absolute path in ASP.Net Core alternative way for Server.MapPath(如何在 ASP.Net Core 替代方式中获取 Server.MapPath 的绝对路径)

          <tfoot id='6xUtr'></tfoot>

            <bdo id='6xUtr'></bdo><ul id='6xUtr'></ul>
              <tbody id='6xUtr'></tbody>

            • <i id='6xUtr'><tr id='6xUtr'><dt id='6xUtr'><q id='6xUtr'><span id='6xUtr'><b id='6xUtr'><form id='6xUtr'><ins id='6xUtr'></ins><ul id='6xUtr'></ul><sub id='6xUtr'></sub></form><legend id='6xUtr'></legend><bdo id='6xUtr'><pre id='6xUtr'><center id='6xUtr'></center></pre></bdo></b><th id='6xUtr'></th></span></q></dt></tr></i><div id='6xUtr'><tfoot id='6xUtr'></tfoot><dl id='6xUtr'><fieldset id='6xUtr'></fieldset></dl></div>
              1. <small id='6xUtr'></small><noframes id='6xUtr'>

              2. <legend id='6xUtr'><style id='6xUtr'><dir id='6xUtr'><q id='6xUtr'></q></dir></style></legend>

                  本文介绍了如何在 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 的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                  Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                  C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                  Multi-color TextBox C#(多色文本框 C#)
                  How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                  C# Numeric Only TextBox Control(C# 纯数字文本框控件)
                      <bdo id='tL0qt'></bdo><ul id='tL0qt'></ul>

                        <tfoot id='tL0qt'></tfoot>
                        <legend id='tL0qt'><style id='tL0qt'><dir id='tL0qt'><q id='tL0qt'></q></dir></style></legend>

                          <i id='tL0qt'><tr id='tL0qt'><dt id='tL0qt'><q id='tL0qt'><span id='tL0qt'><b id='tL0qt'><form id='tL0qt'><ins id='tL0qt'></ins><ul id='tL0qt'></ul><sub id='tL0qt'></sub></form><legend id='tL0qt'></legend><bdo id='tL0qt'><pre id='tL0qt'><center id='tL0qt'></center></pre></bdo></b><th id='tL0qt'></th></span></q></dt></tr></i><div id='tL0qt'><tfoot id='tL0qt'></tfoot><dl id='tL0qt'><fieldset id='tL0qt'></fieldset></dl></div>
                        1. <small id='tL0qt'></small><noframes id='tL0qt'>

                              <tbody id='tL0qt'></tbody>