随笔 - 165, 文章 - 0, 评论 - 18, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

asp.net core 中的各种路径

Posted on   火冰·瓶  阅读(3534)  评论(0编辑  收藏  举报

1.获取完整网址URL

方法一:先引用“using Microsoft.AspNetCore.Http.Extensions;”,然后直接用“Request.GetDisplayUrl();”

方法二:后来参考 Microsoft.AspNetCore.Rewrite 的源代码,写了一个扩展方法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace Microsoft.AspNetCore.Http
{
    public static class HttpRequestExtensions
    {
        public static string GetAbsoluteUri(this HttpRequest request)
        {
            return new StringBuilder()
                .Append(request.Scheme)
                .Append("://")
                .Append(request.Host)
                .Append(request.PathBase)
                .Append(request.Path)
                .Append(request.QueryString)
                .ToString();
        }
    }
}

 

2.获取网站在服务器的物理路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class HomeController : Controller
{
    IHostingEnvironment env;
    public HomeController(IHostingEnvironment _env) { env = _env; }
 
    public IActionResult Index()
    {
        //https://localhost:44359/home/index
        string url = HttpContext.Request.GetDisplayUrl();
 
        //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\bin\\Debug\\netcoreapp2.1\\
        string path = System.AppContext.BaseDirectory;
 
        //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI
        string siteRoot = env.ContentRootPath;
 
        //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\wwwroot
        string wwwroot = env.WebRootPath;
 
        //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\bin\\Debug\\netcoreapp2.1
        var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
         
        return View();
    }
}

  

 

3.获取物理路径

1
string Paths = Path.GetFullPath("..");

  

4.ASP.NET总结C#中7种获取当前路径的方法

 

1
2
3
4
5
6
7
8
AppDomain.CurrentDomain.BaseDirectory=D:\lindexi\dotnet 获取程序所在路径的方法\
Environment.CurrentDirectory=D:\lindexi\dotnet 获取程序所在路径的方法
Assembly.GetCallingAssembly().Location=D:\lindexi\dotnet 获取程序所在路径的方法\SetereBojerhis.exe
Assembly.GetEntryAssembly().Location=D:\lindexi\dotnet 获取程序所在路径的方法\SetereBojerhis.exe
Assembly.GetExecutingAssembly().Location=D:\lindexi\dotnet 获取程序所在路径的方法\SetereBojerhis.exe
Directory.GetCurrentDirectory()=D:\lindexi\dotnet 获取程序所在路径的方法
AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\lindexi\dotnet 获取程序所在路径的方法\
Process.GetCurrentProcess().MainModule.FileName=D:\lindexi\dotnet 获取程序所在路径的方法\SetereBojerhis.exe

 

  

 

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示