如何从URL中获取.aspx页面名称?

时间:2009-12-02 14:40:17

标签: asp.net

我可以使用哪个对象从URL获取当前的PageName.aspx(包括扩展名.aspx)?当我在页面上时,我无法找到允许我抓住它的对象和方法。

8 个答案:

答案 0 :(得分:15)

请注意,有时,在像GoDaddy这样的共享托管上,您可能没有权限创建新的FileInfo对象。是的,相信它。

所以我建议您使用此代码段:

string fullPath = /* System.Web.HttpContext.Current. (optional in most cases) */ Request.Url.AbsolutePath;
string fileName = System.IO.Path.GetFileName(fullPath);

享受: - )

答案 1 :(得分:13)

Pino这里是来源lil man:http://www.devx.com/tips/Tip/42433

  string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
    System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath);
    string sPageName = oFileInfo.Name;

答案 2 :(得分:4)

http://www.aspcode.net/Get-current-page-name.aspx

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; 
} 

答案 3 :(得分:2)

Request.Url.AbsolutePath

拆分'/',最后一项是你的文件名。

答案 4 :(得分:1)

Path.GetFileName(Request.PhysicalPath)可用于获取实际文件名

答案 5 :(得分:0)

我使用了:Request.Url.LocalPath,即使完整的URL为https://www.example.com/?foo=bar,它也给了我“ /default.aspx”。您只需要删除开头的/

答案 6 :(得分:-2)

这个怎么样:

    var pageName = System.IO.Path.GetFileName(Request.Url.ToString());
    Response.Write(pageName);

答案 7 :(得分:-2)

string pageName = Path.GetFileName(Request.Path);