从 ASP.NET 5 controller VS 2015 获取 wwwroot 文件夹路径

Get wwwroot folder path from ASP.NET 5 controller VS 2015

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

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 命名空间获得。

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

您需要将 IWebHostEnvironment 注入您的 class 才能访问 ApplicationBasePath 属性 值:阅读有关 Dependency Injection 的信息。成功注入依赖项后,wwwroot 路径 应该可供您使用。例如:

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 取代

还有另一种方法可以从启动时就实现这一点。这本身并不是这种情况的确切解决方案,但我对其进行了修改以满足我的需要。为此,我们需要一个单身人士。这是一个带有环境注入的初创公司class。

namespace www
{
    public class Startup
    {
        private IHostingEnvironment _env;

        public Startup(IHostingEnvironment env)
        {
            _env = env;
            Environment.rootPath = env.WebRootPath;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseStaticFiles();
            app.UseMvc();
        }
    }
}

我要找的环境变量就在那里,IHOstingEnvironment。但出于我的目的,我需要像这样创建 Environment class 以便轻松访问所有项目中的所有环境项。这个单例将为配置、环境和许多其他事物提供数据。但是对于这个线程,我只会放一个 rootPath 属性.

namespace www.Utilities
{
    public class Environment
    {
        private static Environment instance;
        private static String _rootPath;

        private Environment() { }

        public static Environment Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Environment();
                }
                return instance;
            }
        }

        public static string rootPath
        {
            set
            {
                _rootPath = value;
            }
            get
            {
                return _rootPath;
            }
        }    
    }
}

由于我已经接受了 Oluwafemi 的回答,所以我会保持这种状态,因为他引导我做到了这一点。但我认为这是在整个项目中访问环境变量的更好方法

我知道这已经得到解答,但它根据我的托管环境(IIS Express 与 IIS)给出了不同的结果。如果您想获取 wwwroot 路径 (see this GitHub issue).

,以下方法似乎适用于所有托管环境

例如

private readonly IHostingEnvironment _hostEnvironment;

public ProductsController(IHostingEnvironment hostEnvironment)
{
   _hostEnvironment = hostEnvironment;
}

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

对于那些部署到 Azure 并遇到此错误的人。我所做的是解决它,我没有试图弄清楚为什么 Azure 环境与本地 IIS 不同。 这是我的解决方法:

if (string.IsNullOrWhiteSpace(_environment.WebRootPath))
{
   _environment.WebRootPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}

当然你需要:

private IHostingEnvironment _environment;

public OnboardingController(IHostingEnvironment environment)
{
     _environment = environment;
}