ASP.NET获取URL方法汇总
//获取完整url(协议名+域名+站点名+文件名+参数)
stringfullUrl=Request.Url.ToString();
//获取客户端请求的URL信息(不包括主机和端口)
stringrawUrl=Request.RawUrl;
//获取站点名+页面名
stringabsolutePath=Request.Url.AbsolutePath;
//获取主机部分
stringurlHost=Request.Url.Host;
//获取参数部分
stringurlQuery=Request.Url.Query;
//获取服务器上ASP.NET应用程序的虚拟路径
stringApplicationPath=Request.ApplicationPath;
//获取当前请求的虚拟路径
stringCurrentExecutionFilePath=Request.CurrentExecutionFilePath;
//获取当前请求的虚拟路径
stringPath=Request.Path;
//获取具有URL扩展名的资源的附加路径信息
stringPathInfo=Request.PathInfo;
//获取与请求的URL相对应的物理文件系统路径
stringPhysicalPath=Request.PhysicalPath;
//获取文件名的本地操作系统表示形式
stringLocalPath=Request.Url.LocalPath;
//获取绝对URL
stringAbsoluteUri=Request.Url.AbsoluteUri;
完整代码演示
StringBuildersb=newStringBuilder(); sb.Append("获取完整url(协议名+域名+站点名+文件名+参数):"+fullUrl+"<br/>"); sb.Append("获取客户端请求的URL信息(不包括主机和端口):"+rawUrl+"<br/>"); sb.Append("获取站点名+页面名:"+absolutePath+"<br/>"); sb.Append("获取主机部分:"+urlHost+"<br/>"); sb.Append("获取参数部分:"+urlQuery+"<br/>"); sb.Append("获取应用程序的虚拟应用程序根路径:"+ApplicationPath+"<br/>"); sb.Append("获取当前请求的虚拟路径:"+Path+"<br/>"); sb.Append("获取具有URL扩展名的资源的附加路径信息:"+PathInfo+"<br/>"); sb.Append("获取与请求的URL相对应的物理文件系统路径:"+PhysicalPath+"<br/>"); sb.Append("获取文件名的本地操作系统表示形式:"+LocalPath+"<br/>"); sb.Append("获取绝对URL:"+AbsoluteUri+"<br/>"); Response.Write(sb.ToString());