问题描述
对不起,一个noob问题,但似乎我无法从控制器得到使用Server.Mappath。我需要输出的JSON文件列表从图像中的wwwroot文件夹。他们是wwwroot文件/图像。我怎样才能得到一个可靠的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
推荐答案
您将需要注入 IApplicationEnvironment
进入你的类可以访问 ApplicationBasePath
属性值:阅读关于Dependency注射。成功注入的依赖,在 wwwroot的路径后,应提供给您。例如:
You will need to inject IApplicationEnvironment
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 IApplicationEnvironment _appEnvironment;
public ProductsController(IApplicationEnvironment appEnvironment)
{
_appEnvironment = appEnvironment;
}
用法:
[HttpGet]
public IEnumerable<string> Get()
{
FolderScanner scanner = new FolderScanner(_appEnvironment.ApplicationBasePath);
return scanner.scan();
}
这篇关于获得ASP.NET 5控制器VS 2015年wwwroot文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!