
1 /// <summary>
2 /// 取得网站的根目录的URL
3 /// </summary>
4 /// <returns></returns>
5 public static string GetRootURI()
6 {
7 string AppPath = "";
8 HttpContext HttpCurrent = HttpContext.Current;
9 HttpRequest Req;
10 if (HttpCurrent != null)
11 {
12 Req = HttpCurrent.Request;
13
14 string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
15 if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
16 //直接安装在 Web 站点
17 AppPath = UrlAuthority;
18 else
19 //安装在虚拟子目录下
20 AppPath = UrlAuthority + Req.ApplicationPath;
21 }
22 return AppPath;
23 }
24 /// <summary>
25 /// 取得网站的根目录的URL
26 /// </summary>
27 /// <param name="Req"></param>
28 /// <returns></returns>
29 public static string GetRootURI(HttpRequest Req)
30 {
31 string AppPath = "";
32 if (Req != null)
33 {
34 string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
35 if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
36 //直接安装在 Web 站点
37 AppPath = UrlAuthority;
38 else
39 //安装在虚拟子目录下
40 AppPath = UrlAuthority + Req.ApplicationPath;
41 }
42 return AppPath;
43 }
44 /// <summary>
45 /// 取得网站根目录的物理路径
46 /// </summary>
47 /// <returns></returns>
48 public static string GetRootPath()
49 {
50 string AppPath = "";
51 HttpContext HttpCurrent = HttpContext.Current;
52 if (HttpCurrent != null)
53 {
54 AppPath = HttpCurrent.Server.MapPath("~");
55 }
56 else
57 {
58 AppPath = AppDomain.CurrentDomain.BaseDirectory;
59 if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
60 AppPath = AppPath.Substring(0, AppPath.Length - 1);
61 }
62 return AppPath;
63 }

【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤