ASP.NET中的n种路径

来源:转载

在web服务器端开发中经常会遇到各种不同形式的path,而且经常记不清楚如何获取到某种特定格式,今天抽了点时间把常用的path获取方法与格式做了一个简单的对照表,以备日后查用。

Request获取的信息

Request.AppRelativeCurrentExecutionFilePath                  ~/SysOption/BillingSetup1.aspx

Request.ApplicationPath                                                 /

Request.CurrentExecutionFilePath                                   /SysOption/BillingSetup1.aspx

Request.FilePath                                                            /SysOption/BillingSetup1.aspx

Request.Path                                                                /SysOption/BillingSetup1.aspx

Request.PathInfo                                                          什么都没有(待测其他用法)

Request.PhysicalApplicationPath                               D:/VssWorkFolder/British_School_MIS/src/WebSite/

Request.PhysicalPath                                                      D:/VssWorkFolder/British_School_MIS/src/WebSite/SysOption/BillingSetup1.aspx

Request.UserHostAddress                                              192.168.1.6

Request.UserHostName                                                  192.168.1.6

Request.Url.ToString()                                                    http://192.168.1.6/SysOption/BillingSetup1.aspx?x=d

Request.UrlReferrer                                                        空引用或者跳转源页面地址        

 

Request.ServerVariables集合中获取到的相关信息:

左列是服务器变量名,右侧是值,值是通过Request.ServerVariables[服务器变量名]获取的

APPL_MD_PATH : /LM/W3SVC/894523/Root

APPL_PHYSICAL_PATH : D:/VssWorkFolder/British_School_MIS/src/WebSite/

INSTANCE_META_PATH : /LM/W3SVC/894523

LOCAL_ADDR : 192.168.1.6

PATH_INFO : /SysOption/BillingSetup1.aspx

PATH_TRANSLATED : D:/VssWorkFolder/British_School_MIS/src/WebSite/SysOption/BillingSetup1.aspx

REMOTE_ADDR : 192.168.1.6

REMOTE_HOST : 192.168.1.6

SCRIPT_NAME : /SysOption/BillingSetup1.aspx

SERVER_NAME : 192.168.1.6

URL : /SysOption/BillingSetup1.aspx

Request.ServerVariables是一个很强大的工具,可以帮助我们获取很多client和web宿主的信息,有兴趣的朋友可以通过以下代码看看它到底包含什么信息

        foreach (string s in Request.ServerVariables)

        {

            Response.Write(s + "  :  " + Request.ServerVariables[s] + "");

        }

path转换

1.转换为服务器端路径(Server.MapPath)

web服务器端开发设计一个有趣的问题就是,地址转换。比如http地址/images/a.txt,如果你想在服务器端通过io读取这个文件,就得有这个文件的“本机地址(形如c:/windows/system32/xx.dll)”,这时Server.MapPath就很有用了

Response.Write(Request.MapPath(Request.Path));        输出为 D:/VssWorkFolder/British_School_MIS/src/WebSite/SysOption/BillingSetup1.aspx

2.转换为http地址(Page.ResolveClientUrl Page.ResolveUrl)

Response.Write(Page.ResolveClientUrl("~/a/a.jpg"));      输出为 ../a/a.jpg

Response.Write(Page.ResolveUrl("~/a/a.jpg"));              输出为 /a/a.jpg

分享给朋友:
------分隔线----------------------------
您可能感兴趣的文章:
随机阅读: