我想编写一个c#方法来检索当前页面。例如Default6.aspx我知道我可以做以下事情:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
但我如何获得Default6.aspx?如果网址为http://localhost:1302/TESTERS/,我的方法应返回default.aspx
答案 0 :(得分:46)
Path.GetFileName( Request.Url.AbsolutePath )
答案 1 :(得分:12)
您需要的课程是 System.Uri
Dim url As System.Uri = Request.UrlReferrer
Debug.WriteLine(url.AbsoluteUri) ' => http://www.mysite.com/default.aspx
Debug.WriteLine(url.AbsolutePath) ' => /default.aspx
Debug.WriteLine(url.Host) ' => http:/www.mysite.com
Debug.WriteLine(url.Port) ' => 80
Debug.WriteLine(url.IsLoopback) ' => False
答案 2 :(得分:6)
试试这个:
path.Substring(path.LastIndexOf("/");
答案 3 :(得分:5)
如下所示的简单功能将有所帮助:
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
答案 4 :(得分:1)
你可以在下面试试。
string url = "http://localhost:1302/TESTERS/Default6.aspx";
string fileName = System.IO.Path.GetFileName(url);
希望这有帮助。
答案 5 :(得分:1)
Request.Url.Segments.Last()
另一种选择。