You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB
C#

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;
}
/// <summary>
/// 获取服务器文件路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string MapPath(string path)
{
return MapPath(path, false);
}
/// <summary>
/// 获取wwwroot路径
/// </summary>
/// <param name="path"></param>
/// <param name="rootPath">是否获取wwwroot路径</param>
/// <returns></returns>
public string MapPath(string path, bool rootPath)
{
if (rootPath)
{
return Path.Combine(_hostingEnvironment.ContentRootPath, "wwwroot").ReplacePath();
}
return Path.Combine(_hostingEnvironment.ContentRootPath, path).ReplacePath();
}
}
}