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

后端 未结 4 1755
名媛妹妹
名媛妹妹 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 03:06

    You will need to inject IHostingEnvironment 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 IHostingEnvironment _appEnvironment;
    
    public ProductsController(IHostingEnvironment appEnvironment)
    {
       _appEnvironment = appEnvironment;
    }
    

    Usage:

     [HttpGet]
     public IEnumerable Get()
     {
        FolderScanner scanner = new FolderScanner(_appEnvironment.ApplicationBasePath);
        return scanner.scan();
     }
    

提交回复
热议问题