.NET 关于取当前地址的问题

用Request.CurrentExecutionFilePath,就只能取到www.5ixznr.cn/index.aspx

用Request.RawUrl就可以把index.aspx??boardid=22&page=1全取出来  

在ASP.NET编程中经常需要用Request获取url的有关信息,Request中有多种方法获取url信息.测试的url地址是http://www.test.com/testweb/default.aspx?xx=xx, 结果如下:


Request.ApplicationPath: /testweb
Request.CurrentExecutionFilePath: /testweb/default.aspx
Request.FilePath: /testweb/default.aspx
Request.Path: /testweb/default.aspx
Request.PathInfo:
Request.PhysicalApplicationPath: E:\WWW\testweb\
Request.PhysicalPath: E:\WWW\testweb\default.aspx
Request.RawUrl: /testweb/default.aspx?xx=xx
Request.Url.AbsolutePath: /testweb/default.aspx
Request.Url.AbsoluteUri: http://www.test.com/testweb/default.aspx
Request.Url.Host: www.test.com
Request.Url.LocalPath: /testweb/default.aspx  

//---------------------------以下代码未测试------------------

private string GetCurrentQueryParams()
{
StringBuilder stringBuilder = new StringBuilder();
string currentPath = HttpContext.Current.Request.Url.PathAndQuery;
int startIndex = currentPath.IndexOf("?");
if (startIndex <= 0)
return string.Empty;
string[] nameValues = currentPath.Substring(startIndex + 1).Split('&');
foreach ( string param in nameValues )
{
stringBuilder.Append( param );
stringBuilder.Append( "&" );
}
return stringBuilder.ToString().TrimEnd( '&' );
}

原文地址:https://www.cnblogs.com/Byrd/p/2455233.html