asp.net 获取URL的可靠方法
本文导语: 假如,当前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.P...
假如,当前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.CurrentExecutionFilePath: /testweb/default.aspx
Request.FilePath: /testweb/default.aspx
Request.Path: /testweb/default.aspx
Request.PhysicalApplicationPath: E:WWWtestwebRequest.PhysicalPath: E:WWWtestwebdefault.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 );