using Common.Extensions; using Microsoft.Extensions.Hosting; using System.IO; namespace Common.Provider { public interface IPathProvider { string MapPath(string path); string MapPath(string path, bool rootPath); IHostEnvironment GetHostingEnvironment(); } public class PathProvider : IPathProvider { private IHostEnvironment _hostingEnvironment; public PathProvider(IHostEnvironment environment) { _hostingEnvironment = environment; } public IHostEnvironment GetHostingEnvironment() { return _hostingEnvironment; } /// /// 获取服务器文件路径 /// /// /// public string MapPath(string path) { return MapPath(path, false); } /// /// 获取wwwroot路径 /// /// /// 是否获取wwwroot路径 /// public string MapPath(string path, bool rootPath) { if (rootPath) { return Path.Combine(_hostingEnvironment.ContentRootPath, "wwwroot").ReplacePath(); } return Path.Combine(_hostingEnvironment.ContentRootPath, path).ReplacePath(); } } }