介绍一下ASP.NET取得当前页面的完整URL 的方放

[csharp] view plaincopy
01.private string GetPath()  
02.    {  
03.        string strPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.ServerVariables["PATH_INFO"] + "?" + Request.ServerVariables["QUERY_STRING"];  
04.        if(strPath.EndsWith("?"))  
05.        {  
06.            strPath = strPath.Substring(0, strPath.Length - 1);  
07.        }  
08.        return strPath;  
09.    }  



 asp.net获取URL和IP地址 
HttpContext.Current.Request.Url.ToString() 并不可靠。

如果当前URL为 
http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5

通过HttpContext.Current.Request.Url.ToString()获取到的却是 

http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=¼¼Êõ


正确的方法是:HttpContext.Current.Request.Url.PathAndQuery1、通过ASP.NET获取
 如果测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath:                 /testweb
 Request.CurrentExecutionFilePath:        /testweb/default.aspx
 Request.FilePath:                        /testweb/default.aspx
 Request.Path:                            /testweb/default.aspx
 Request.PhysicalApplicationPath:         E:/WWW/testwebRequest.PhysicalPath:                    E:/WWW/testweb/default.aspx
 Request.RawUrl:                          /testweb/default.aspx
 Request.Url.AbsolutePath:                /testweb/default.aspx
 Request.Url.AbsoluteUrl:                 http://www.test.com/testweb/default.aspx
 Request.Url.Host:                       http://www.test.com/
 Request.Url.LocalPath:                   /testweb/default.aspx 

2、通过JS获取

 



[javascript] view plaincopy
01.<table width=100% cellpadding=0 cellspacing=0 border=0 >  
02.  
03.<script>   
04.  
05.thisURL = document.URL;   
06.  
07.thisHREF = document.location.href;   
08.  
09.thisSLoc = self.location.href;   
10.  
11.thisDLoc = document.location;   
12.  
13.strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>"   
14.  
15.strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>"  
16.  
17.strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>"  
18.  
19.strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>"  
20.  
21.document.write( strwrite );   
22.  
23.</script>   
24.  
25.thisDLoc = document.location; <BR>  
26.  
27.thisURL = document.URL; <BR>  
28.  
29.thisHREF = document.location.href; <BR>  
30.  
31.thisSLoc = self.location.href;<BR>  
32.  
33.<script>   
34.  
35.thisTLoc = top.location.href;   
36.  
37.thisPLoc = parent.document.location;   
38.  
39.thisTHost = top.location.hostname;   
40.  
41.thisHost = location.hostname;   
42.  
43.strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>"  
44.  
45.strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>"  
46.  
47.strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>"  
48.  
49.strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>"  
50.  
51.document.write( strwrite );   
52.  
53.</script>   
54.  
55.thisTLoc = top.location.href; <BR>  
56.  
57.thisPLoc = parent.document.location; <BR>  
58.  
59.thisTHost = top.location.hostname; <BR>  
60.  
61.thisHost = location.hostname;<BR>  
62.  
63.<script>   
64.  
65.tmpHPage = thisHREF.split( "/" );   
66.  
67.thisHPage = tmpHPage[ tmpHPage.length-1 ];   
68.  
69.tmpUPage = thisURL.split( "/" );   
70.  
71.thisUPage = tmpUPage[ tmpUPage.length-1 ];   
72.  
73.strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>"  
74.  
75.strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>"  
76.  
77.document.write( strwrite );   
78.  
79.</script><tr><td>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值