Files
zszq-trs/YLErpWeb/App/Server.cs
T
2024-05-09 14:06:26 +08:00

96 lines
3.0 KiB
C#

using YLErp.Providers;
namespace YLErp.Web
{
public static class Server
{
static string _webRootPath;
static string _virtualRootPath;
static Server()
{
CacheProvider = new MemoryCacheProvider();
}
public static void Initialize(IWebHostEnvironment environment, ConfigurationManager configuration)
{
if (string.IsNullOrWhiteSpace(environment.WebRootPath))
{
environment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
}
_webRootPath = environment.WebRootPath;
var appSettings = configuration.GetSection("AppSettings");
_virtualRootPath = appSettings.GetValue<string>("VirtualPathRoot");
if (string.IsNullOrEmpty(_virtualRootPath))
{
var dir = new DirectoryInfo(environment.ContentRootPath);
_virtualRootPath = Path.Combine(dir.Parent.FullName, Path.GetDirectoryName(environment.ContentRootPath) + "_v");
if (!Directory.Exists(_virtualRootPath))
{
_virtualRootPath = environment.ContentRootPath;
}
Environment.SetEnvironmentVariable("虚拟目录根路径", _virtualRootPath);
}
RazorEngine = new RazorLight.RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(typeof(Program))
.SetOperatingAssembly(typeof(Program).Assembly)
.UseMemoryCachingProvider()
.Build();
}
/// <summary>
/// RazorEngine
/// </summary>
public static RazorLight.RazorLightEngine RazorEngine { get; private set; }
/// <summary>
///
/// </summary>
public static bool UseRightAligned { get; set; }
/// <summary>
/// 缓存
/// </summary>
public static readonly MemoryCacheProvider CacheProvider;
static readonly char[] MapPathTrim = new[] { '~', '/', '\\' };
/// <summary>
/// 把指定的路径映射到服务器上相应的物理路径上
/// </summary>
public static string MapPath(string path)
{
if (string.IsNullOrEmpty(path))
{
return _webRootPath;
}
if (OperatingSystem.IsWindows())
{
path = path.TrimStart(MapPathTrim).Replace('/', '\\');
}
else
{
path = path.TrimStart(MapPathTrim).Replace('\\', '/');
}
if (path.StartsWith("App_", StringComparison.OrdinalIgnoreCase)
|| path.Equals("Scripts") || path.Equals("Style"))
{
return Path.Combine(_virtualRootPath, path);
}
return Path.Combine(_webRootPath, path);
}
/// <summary>
/// 虚拟目录根路径
/// </summary>
public static string GetVirtualRootPath() => _virtualRootPath;
}
}