如何在asp.net中获取URL和IP地址?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
![](/upload/ad_content/xuanchuantu-21.jpg)
10年积累的
做网站、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先
制作网站后付款的网站建设流程,更有
蒙自免费网站建设让你可以放心的选择与我们合作。
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.PathAndQuery
1、通过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: www.test.com
Request.Url.LocalPath: /testweb/default.aspx
2、通过JS获取
复制代码 代码如下:
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = "
thisURL: | [" + thisURL + "] |
"strwrite += "
thisHREF: | [" + thisHREF + "] |
"strwrite += "
thisSLoc: | [" + thisSLoc + "] |
"strwrite += "
thisDLoc: | [" + thisDLoc + "] |
"document.write( strwrite );
thisDLoc = document.location;
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = "
thisTLoc: | [" + thisTLoc + "] |
"strwrite += "
thisPLoc: | [" + thisPLoc + "] |
"strwrite += "
thisTHost: | [" + thisTHost + "] |
"strwrite += "
thisHost: | [" + thisHost + "] |
"document.write( strwrite );
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
tmpHPage = thisHREF.split( "/" );
thisHPage = tmpHPage[ tmpHPage.length-1 ];
tmpUPage = thisURL.split( "/" );
thisUPage = tmpUPage[ tmpUPage.length-1 ];
strwrite = "
thisHPage: | [" + thisHPage + "] |
"strwrite += "
thisUPage: | [" + thisUPage + "] |
"document.write( strwrite );
================= 获取IP 1、ASP.NET中获取获取服务器的IP地址:
复制代码 代码如下: using System.Net;
string myIP,myMac; System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; if ( addressList.Length>1) { myIP = addressList[0].ToString(); myMac = addressList[1].ToString(); } else { myIP = addressList[0].ToString(); myMac = "没有可用的连接"; }
myIP地址就是服务器端的ip地址。 获取客户端的ip地址,可以使用
复制代码 代码如下: //获取登录者ip地址 string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
2、通过JS获取
复制代码 代码如下:
myForm.macAddress.value=sMacAddr; myForm.ipAddress.value=sIPAddr; myForm.hostName.value=sDNSName;
关于如何在asp.net中获取URL和IP地址问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
网站名称:如何在asp.net中获取URL和IP地址-创新互联
浏览路径:http://ncjierui.cn/article/edcdi.html
|