从网址获取当前页面

ama*_*eur 35 c# url request

我想写ac#方法来检索当前页面.例如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
Run Code Online (Sandbox Code Playgroud)

但是我如何获得Default6.aspx?如果url是http:// localhost:1302/TESTERS /,我的方法应该返回default.aspx

Pau*_*der 46

Path.GetFileName( Request.Url.AbsolutePath )
Run Code Online (Sandbox Code Playgroud)

  • 认为应该是'Path.GetFileName(Request.Url.AbsolutePath)' (6认同)

ace*_*33s 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
Run Code Online (Sandbox Code Playgroud)

http://www.devx.com/vb2themax/Tip/18709


nrp*_*rph 6

试试这个:

path.Substring(path.LastIndexOf("/");
Run Code Online (Sandbox Code Playgroud)


Nei*_*ght 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; 
} 
Run Code Online (Sandbox Code Playgroud)