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


sorry noob question, seems can't server.mappath controller. need output json file list images folder @ wwwroot. @ wwwroot/images. how can 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 seems not available system.web namespace.

project using asp.net 5 , dotnet 4.6 framework

you need inject ihostingenvironment class have access applicationbasepath property value: read dependency injection. after injecting dependency, wwwroot path should available you. example:

private readonly ihostingenvironment _appenvironment;  public productscontroller(ihostingenvironment appenvironment) {    _appenvironment = appenvironment; } 

usage:

 [httpget]  public ienumerable<string> get()  {     folderscanner scanner = new folderscanner(_appenvironment.applicationbasepath);     return scanner.scan();  } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -