介绍一下ASP.NET取得当前页面的完整URL 的方放,icech做成了函数,直接用吧!
1 private string GetPath() 2 { 3 string strPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.ServerVariables["PATH_INFO"] + "?" + Request.ServerVariables["QUERY_STRING"]; 4 if(strPath.EndsWith("?")) 5 { 6 strPath = strPath.Substring(0, strPath.Length - 1); 7 } 8 return strPath; 9 }
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获取
1 <table width=100% cellpadding=0 cellspacing=0 border=0 > 2 3 <script> 4 5 thisURL = document.URL; 6 7 thisHREF = document.location.href; 8 9 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> 80 81 ================= 82 获取IP 83 1、ASP.NET中获取 84 85 获取服务器的IP地址: 86 using System.Net; 87 88 string myIP,myMac; 89 System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; 90 if ( addressList.Length>1) 91 { 92 myIP = addressList[0].ToString(); 93 myMac = addressList[1].ToString(); 94 } 95 else 96 { 97 myIP = addressList[0].ToString(); 98 myMac = "没有可用的连接"; 99 }