服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - C#获取当前页面的URL示例代码

C#获取当前页面的URL示例代码

2019-11-20 14:05C#教程网 ASP.NET教程

获取当前页面的URL在某些情况下还是比较使用的,下面为大家介绍下如何通过C#以及Javascript获取当前页面的URL,感兴趣的朋友不要错过

本实例的测试URL:http://www.mystudy.cn/web/index.aspx 

1、通过C#获取当前页面的URL 

复制代码代码如下:


string url = Request.Url.AbsoluteUri; //结果: http://www.mystudy.cn/web/index.aspx 
string host = Request.Url.Host; //结果:www.mystudy.cn 
string rawUrl = Request.RawUrl; //结果:/web/index.aspx 
string localPath = Request.Url.LocalPath; //结果:/web/index.aspx 


2、通过Javascript获取当前页面的URL 

复制代码代码如下:


var url = document.URL; //结果:http://www.mystudy.cn/web/index.aspx 
var href = document.location.href; //结果:http://www.mystudy.cn/web/index.aspx 
var host = location.hostname; //结果:www.mystudy.cn 

延伸 · 阅读

精彩推荐