您的位置:首页 > 编程学习 > ASP.NET

Request获取URL的各种信息

更多 2017/1/10 来源:ASP.NET学习浏览量:1249
学习标签: Request
本文导读:在ASP.NET开发中,经常需要获取URL的各种信息,例如,URL的域名、URL对应的物理路径、URL后面的参数等等其它信息,ASP.NET中Request提供了多种属性,能够方便的获取到URL的各种信息。

以 http://localhost:51566/test/Default.aspx?id=56 为例

 
 
C# 代码   复制

        //获取完整url (协议名+域名+站点名+文件名+参数)

        string fullUrl = Request.Url.ToString();

        //获取客户端请求的URL信息(不包括主机和端口)

        string rawUrl = Request.RawUrl;

        //获取站点名+页面名

        string absolutePath = Request.Url.AbsolutePath;

        //获取主机部分

        string urlHost = Request.Url.Host;

        //获取参数部分

        string urlQuery = Request.Url.Query;

        //获取服务器上ASP.NET应用程序的虚拟路径

        string ApplicationPath = Request.ApplicationPath;

        //获取当前请求的虚拟路径

        string CurrentExecutionFilePath = Request.CurrentExecutionFilePath;

        //获取当前请求的虚拟路径

        string Path = Request.Path;

        //获取具有URL扩展名的资源的附加路径信息

        string PathInfo = Request.PathInfo;

        //获取与请求的URL相对应的物理文件系统路径

        string PhysicalPath = Request.PhysicalPath;

        //获取文件名的本地操作系统表示形式

        string LocalPath = Request.Url.LocalPath;

        //获取绝对URL

        string AbsoluteUri = Request.Url.AbsoluteUri;


        StringBuilder sb = new StringBuilder();

        sb.Append("Request.Url.ToString():  " + fullUrl + "<br />");

        sb.Append("Request.RawUrl:  " + rawUrl + "<br />");

        sb.Append("Request.Url.AbsolutePath:  " + absolutePath + "<br />");

        sb.Append("Request.Url.Host:  " + urlHost + "<br />");

        sb.Append("Request.Url.Query:  " + urlQuery + "<br />");

        sb.Append("Request.ApplicationPath:  " + ApplicationPath + "<br />");

        sb.Append("Request.CurrentExecutionFilePath:  " + CurrentExecutionFilePath + "<br />");

        sb.Append("Request.Path:  " + Path + "<br />");

        sb.Append("Request.PathInfo:  " + PathInfo + "<br />");

        sb.Append("Request.PhysicalPath:  " + PhysicalPath + "<br />");

        sb.Append("Request.Url.LocalPath:  " + LocalPath + "<br />");

        sb.Append("Request.Url.AbsoluteUri:  " + AbsoluteUri + "<br />");

        Response.Write(sb.ToString());

 

输出结果

 
 


Request.Url.ToString():           http://localhost:51566/test/Default.aspx?id=56

Request.RawUrl:                   /test/Default.aspx?id=56

Request.Url.AbsolutePath:         /test/Default.aspx

Request.Url.Host:                 localhost

Request.Url.Query:                ?id=56

Request.ApplicationPath:          /

Request.CurrentExecutionFilePath: /test/Default.aspx

Request.Path:                     /test/Default.aspx

Request.PathInfo: 

Request.PhysicalPath:             D:\example\WebSite1\test\Default.aspx

Request.Url.LocalPath:            /test/Default.aspx

Request.Url.AbsoluteUri:          http://localhost:51566/test/Default.aspx?id=56

 

收藏
427
很赞
41
您可能感兴趣