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

后端 未结 4 1754
名媛妹妹
名媛妹妹 2020-12-10 02:10

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/im

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 02:46

    I know this has already been answered, but it has given me different results depending on my hosting environment (IIS Express vs IIS). The following approach seems to work for all hosting environments nicely if you want to get your wwwroot path (see this GitHub issue).

    For example

    private readonly IHostingEnvironment _hostEnvironment;
    
    public ProductsController(IHostingEnvironment hostEnvironment)
    {
       _hostEnvironment = hostEnvironment;
    }
    
    [HttpGet]
    public IEnumerable Get()
    {
       FolderScanner scanner = new FolderScanner(_hostEnvironment.WebRootPath);
       return scanner.scan();
    }
    

提交回复
热议问题