ASP.NET与Javascript中获取URL信息

上次看到一篇Making sense of ASP.Net Paths关于.NET中获取URL相关信息的文章,感觉总结的很好,消除了我一些模糊的认识。以前都是要用时在MSDN中找一下Request的文档,对各个属性的差别并不清楚,今天我也把一些常用的属性整理一下,做个总结。如有什么不对的地方,还请大家指教。

我们假设访问这个地址:http://localhost:1351/WebSite1/dir/Default.aspx/adsf?id=12#test ,下面是各个方法的返回值。

ASP.NET

Request PropertyResultRemark
Request.ApplicationPath/WebSite1相对于应用程序(而非虚拟目录)根目录的路径
Request.PhysicalApplicationPathC:\Documents and Settings\wstone.SPANSION\Desktop\WebSite1\应用程序根目录的物理路径
Request.PhysicalPathC:\Documents and Settings\wstone.SPANSION\Desktop\WebSite1\dir\Default.aspx当前请求的物理路径
Request.PathInfo/adsf附加的路径信息
Request.RawUrl/WebSite1/dir/Default.aspx/adsf?id=12当前请求相对于应用程序根目录的路径
Request.Urlhttp://localhost:1351/WebSite1/dir/Default.aspx/adsf?id=12当前请求的完整路径(不包括#之后的)
Request.CurrentExecutionFilePath/WebSite1/dir/Default.aspx
Request.FilePath/WebSite1/dir/Default.aspx
Request.Path/WebSite1/dir/Default.aspx/adsf
Request.ServerVariables["SERVER_NAME"]localhost
Request.ServerVariables["SERVER_PORT"]1351
Request.ServerVariables["SERVER_PORT_SECURE"]0
Server.MapPath("~")C:\Documents and Settings\wstone.SPANSION\Desktop\WebSite1
Server.MapPath("~/")C:\Documents and Settings\wstone.SPANSION\Desktop\WebSite1\
Server.MapPath("~/1.jpg")C:\Documents and Settings\wstone.SPANSION\Desktop\WebSite1\1.jpg
#testASP.NET中不能得到#之后的test,因为#不回发页面

PS:在VS2005中打开WebSite1网站,在浏览器中查看,此时WebSite1为虚拟目录。

Javascript

Location PropertyResult
location.hash#test
location.hostlocalhost:1351
location.hostnamelocalhost
location.hrefhttp://localhost:1351/WebSite1/dir/Default.aspx/adsf?id=12#test
location.pathname/WebSite1/dir/Default.aspx/adsf
location.port1351
location.protocolhttp:
location.search?id=12