ASP.NET 中,Request 有很多带 Path、Url 的属性:
Response.Write("<p>Request.PhysicalApplicationPath:" + Request.PhysicalApplicationPath + "</p>"); Response.Write("<p>Request.PhysicalPath:" + Request.PhysicalPath + "</p>"); Response.Write("<p>Request.CurrentExecutionFilePath:" + Request.CurrentExecutionFilePath + "</p>"); Response.Write("<p>Request.FilePath:" + Request.FilePath + "</p>"); Response.Write("<p>Request.ApplicationPath:" + Request.ApplicationPath + "</p>"); Response.Write("<p>Request.Path:" + Request.Path + "</p>"); Response.Write("<p>Request.RawUrl:" + Request.RawUrl + "</p>"); Response.Write("<p>Request.Url:" + Request.Url + "</p>"); Response.Write("<p>Request.Url.AbsoluteUri:" + Request.Url.AbsoluteUri + "</p>"); Response.Write("<p>Request.Url.AbsolutePath:" + Request.Url.AbsolutePath + "</p>"); Response.Write("<p>Request.Url.LocalPath:" + Request.Url.LocalPath + "</p>"); Response.Write("<p>Request.Url.PathAndQuery:" + Request.Url.PathAndQuery + "</p>"); Response.Write("<p>Request.Url.Query:" + Request.Url.Query + "</p>");
如上,即使如此,都还有很多没有列出,我们先说说环境吧:
我们将程序程序部署在了 D:\app\itpow。
我们并没有将程序放在 IIS 网站根目录,而是放在 test 这个应用程序目录。
我们在 web.config 中使用了 rewrite,将 page-1.htm 映射成了 Page.aspx?id=1。
当访问 page-1.htm 时,得到的结果如下:
Request.PhysicalApplicationPath:D:\app\itpow\(应用程序物理路径)
Request.PhysicalPath:D:\app\itpow\Page.aspx(文件物理路径)
Request.CurrentExecutionFilePath:/test/Page.aspx(含应用程序路径)
Request.FilePath:/test/Page.aspx(含应用程序路径)
Request.ApplicationPath:/test(应用程序路径,如果是放在根目录下,则是:/)
Request.Path:/test/Page.aspx(含应用程序路径)
Request.RawUrl:/test/page-1.htm(映射前的 Path + QueryString)
Request.Url:http://www.itpow.com/test/Page.aspx?id=1(映射后的 Url)
Request.Url.AbsoluteUri:http://www.itpow.com/test/Page.aspx?id=1(映射后的 Url)
Request.Url.AbsolutePath:/test/Page.aspx(含应用程序路径)
Request.Url.LocalPath:/test/Page.aspx(含应用程序路径)
Request.Url.PathAndQuery:/test/Page.aspx?id=1(映射后的 Path + QueryString)
Request.Url.Query:?id=1(映射后的 QueryString)
【控件也是】上面的代码,假如不是放在 Page.aspx 中的,而是放在某 .ascx 控件中的,Page.aspx 引用了这个控件,得到的结论是一样的。
真乱呀,我觉得可以精简一下,关注重点的:
物理路径相关的:
Request.PhysicalApplicationPath:D:\app\itpow\(应用程序物理路径)
Request.PhysicalPath:D:\app\itpow\Page.aspx(文件物理路径)
应用程序相关的:
Request.ApplicationPath:/test(应用程序路径,如果是放在根目录下,则是:/)
Request.Path:/test/Page.aspx(含应用程序路径)
Url 部分相关的:
Request.RawUrl:/test/page-1.htm(映射前的 Path + QueryString)
▲▲▲RawUrl 是字符串噢,不包含前面部分噢。▲▲▲,▼▼▼Url 则是 Uri 类型,当然直接输出也会自动转换成字符串,包含前面部分噢。▼▼▼
Request.Url:http://www.itpow.com/test/Page.aspx?id=1(映射后的 Url)
Request.Url.Scheme:http
Request.Url.Authority:www.itpow.com,假如是 81 端口,则是 www.itpow.com:81
Request.Url.PathAndQuery:/test/Page.aspx?id=1(映射后的 Path + QueryString)
Request.Url.AbsolutePath:/test/Page.aspx(映射后的 Path)
Request.Url.Query:?id=1(映射后的 QueryString)
另请参见:非 80 端口映射到 80 时,.NET 如何获取浏览器中的网址?
访问首页?
访问首页,我们并没有输入 default.aspx,是 IIS 默认交给 default.aspx 处理的,此时:
RawUrl 是不包含 default.aspx 的,即为:/test/
Url 则是包含 default.aspx 的。