• <small id='yg5XV'></small><noframes id='yg5XV'>

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

    1. <legend id='yg5XV'><style id='yg5XV'><dir id='yg5XV'><q id='yg5XV'></q></dir></style></legend>

        从 ASP.NET 5 控制器 VS 2015 获取 wwwroot 文件夹路径

        时间:2023-06-08

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

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

                <bdo id='aQWEy'></bdo><ul id='aQWEy'></ul>
                  <tbody id='aQWEy'></tbody>
                  <tfoot id='aQWEy'></tfoot>
                  <legend id='aQWEy'><style id='aQWEy'><dir id='aQWEy'><q id='aQWEy'></q></dir></style></legend>

                • 本文介绍了从 ASP.NET 5 控制器 VS 2015 获取 wwwroot 文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  很抱歉提出一个菜鸟问题,但我似乎无法从 Controller 获取 Server.MapPath.我需要从 wwwroot 的图像文件夹中输出 json 文件列表.它们位于 wwwroot/images.如何获得可靠的 wwwroot 路径?

                  Sorry for a noob question, but it seems I can't get Server.MapPath from Controller. I need to output json file list from images folder at wwwroot. They are is at wwwroot/images. How can I get a reliable wwwroot path?

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Threading.Tasks;
                  using Microsoft.AspNet.Mvc;
                  using www.Classes;
                  using System.Web;
                  
                  namespace www.Controllers
                  {
                      [Route("api/[controller]")]
                      public class ProductsController : Controller
                      {
                          [HttpGet]
                          public IEnumerable<string> Get()
                          {
                              FolderScanner scanner = new FolderScanner(Server.MapPath("/"));
                              return scanner.scan();
                          }
                      }
                  }
                  

                  Server.MapPath 似乎在 System.Web 命名空间中不可用.

                  Server.MapPath seems not available from System.Web namespace.

                  项目正在使用 ASP.NET 5 和 dotNET 4.6 框架

                  Project is using ASP.NET 5 and dotNET 4.6 Framework

                  推荐答案

                  您需要将 IWebHostEnvironment 注入到您的类中才能访问 ApplicationBasePath 属性值:阅读关于依赖注入.成功注入依赖后,wwwroot 路径 应该可供您使用.例如:

                  You will need to inject IWebHostEnvironment into your class to have access to the ApplicationBasePath property value: Read about Dependency Injection. After successfully injecting the dependency, the wwwroot path should be available to you. For example:

                  private readonly IWebHostEnvironment _appEnvironment;
                  
                  public ProductsController(IWebHostEnvironment appEnvironment)
                  {
                     _appEnvironment = appEnvironment;
                  }
                  

                  用法:

                   [HttpGet]
                   public IEnumerable<string> Get()
                   {
                      FolderScanner scanner = new FolderScanner(_appEnvironment.WebRootPath);
                      return scanner.scan();
                   }
                  

                  IHostingEnvironment 在 asp.net 的更高版本中已被 IWebHostEnvironment 取代.

                  IHostingEnvironment has been replaced by IWebHostEnvironment in later versions of asp.net.

                  这篇关于从 ASP.NET 5 控制器 VS 2015 获取 wwwroot 文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ASP.NET Core 依赖解析器 下一篇:如何为验证属性提供本地化验证消息

                  相关文章

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

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

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