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("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(); } /// /// RazorEngine /// public static RazorLight.RazorLightEngine RazorEngine { get; private set; } /// /// /// public static bool UseRightAligned { get; set; } /// /// 缓存 /// public static readonly MemoryCacheProvider CacheProvider; static readonly char[] MapPathTrim = new[] { '~', '/', '\\' }; /// /// 把指定的路径映射到服务器上相应的物理路径上 /// 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); } /// /// 虚拟目录根路径 /// public static string GetVirtualRootPath() => _virtualRootPath; } }