c# .net获取当前url需要特别注意的方面

本文介绍了如何在程序开发过程中通过Request.Url来获取当前网址的各种组件,包括完整URL、路径、查询参数及端口等,并强调了在使用代理服务器时可能出现的问题。

     在程序开发时通常需要动态的获取当前的网址,不管是在mvc 还是 webform中,都是用Request.Url获取。 

//获取完整url

var uri = Request.Url.ToString();

其中uri的值为 http://www.itxst.com/detail/qqfaqvij.html 

但是需要注意的是

如果使用了代理服务器,那么Request.Url获取的并不是真实的客户端URL地址,可能是 http://192.*.*.*/detail/qqfaqvij.html  所以程序开发时需要特别留神。

//获取 完整地址 (域名+参数)
var foo= Request.Url.ToString();
// 输出 http://www.itxst.com/?from=baidu


//获取目录和参数:
var foo = Request.Url.PathAndQuery;
// 输出  /books/detail.html?id=1111
 
 
//获取 域名:
var host= Request.Url.Host;
// 输出 www.itxst.com 


//获取参数:
var querys= Request.Url.Query;
// 输出  id=1111&title=abc


//获取端口
var  port = Request.Url.Port;

// 输出 80

 

C# 后端(特别是在 ASP.NET 或 ASP.NET Core)中,获取当前请求的 URL 是一个常见需求,比如用于日志记录、重定向、权限验证、生成回调链接等。 下面是 **在 ASP.NET Core** 和 **传统的 ASP.NET MVC** 中获取当前请求 URL 的方式: --- ## ✅ 1. 在 ASP.NET Core 中获取当前请求的 URL 在控制器中,可以通过 `HttpContext.Request` 获取完整的 URL。 ### 示例代码: ```csharp [ApiController] [Route("[controller]")] public class HomeController : ControllerBase { [HttpGet] public IActionResult GetCurrentUrl() { // 获取完整的 URL,包括查询字符串 var request = HttpContext.Request; var currentUrl = $"{request.Scheme}://{request.Host}{request.Path}{request.QueryString}"; return Ok(new { CurrentUrl = currentUrl }); } } ``` ### 输出示例(如果访问的是 `/home?name=test`): ```json { "CurrentUrl": "http://localhost:5000/home?name=test" } ``` ### 各部分说明: - `request.Scheme`:协议(http 或 https) - `request.Host`:主机名和端口(如 localhost:5000) - `request.Path`:请求路径(如 /home/index) - `request.QueryString`:查询字符串(如 ?name=value) --- ## ✅ 2. 在 ASP.NET MVC(非 Core)中获取当前 URL 在传统的 ASP.NET MVC 中,可以通过 `Request.Url` 获取完整的 URL。 ### 示例代码: ```csharp public class HomeController : Controller { public ActionResult Index() { Uri url = Request.Url; string currentUrl = url.ToString(); // 完整的 URL string path = url.AbsolutePath; // 路径部分 string query = url.Query; // 查询字符串部分 ViewBag.CurrentUrl = currentUrl; return View(); } } ``` --- ## ✅ 3. 获取当前请求的相对路径(Path) 如果你想获取不带域名和协议的路径部分(如 `/home/index`),可以使用: ```csharp // ASP.NET Core var path = HttpContext.Request.Path; // ASP.NET MVC var path = Request.Url.AbsolutePath; ``` --- ## ✅ 4. 获取当前 Action 的 URL(带参数) 如果你在控制器中想获取当前 Action 的 URL(包括路由参数),可以使用 `Url.Action()`: ```csharp var url = Url.Action("Index", "Home", null, Request.Scheme); ``` --- ## ✅ 总结 | 用法 | ASP.NET Core | ASP.NET MVC | |------|----------------|--------------| | 完整 URL | `Request.Scheme + "://" + Request.Host + Request.Path + Request.QueryString` | `Request.Url.ToString()` | | 路径部分 | `Request.Path` | `Request.Url.AbsolutePath` | | 查询字符串 | `Request.QueryString` | `Request.Url.Query` | | 构造 Action URL | `Url.Action("Action", "Controller", null, Request.Scheme)` | `Url.Action("Action", "Controller")` | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值