ASP.NET获取URL方法汇总

 更新时间:2021年9月22日 10:08  点击:1570

//获取完整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("获取完整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());

[!--infotagslink--]

相关文章