Get PageName.aspx object from page

I want to get pagename.aspx from the current page object. I do not want to do this through HttpContext.Current. Request , because if I'm already on the page and doing something, why not just grab it from the page ... I don’t need to worry about any context here.

I assume the page already has a name, and I just need to add .aspx, but is there any way to get the extension with it automatically?

+3
source share
5 answers
public string GetCurrentPageName() 
{ 
    string sPath = Request.Url.AbsolutePath;
    System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath); 
    string sRet = oInfo.Name; 
    return sRet; 
} 

(Adapted from http://www.aspcode.net/Get-current-page-name.aspx )

+4
source

I believe this will work (but I have not tested)

 string sRet = System.IO.Path.GetFileName(Request.Url.AbsolutePath)

, Larsenal, , .

+1

It may be too late, but it will help someone else.

var path = Path.GetFileName(Request.PhysicalPath);
0
source

inside the page code,

this.GetType().Name

or

this.GetType().Name.Split('_')[0]
-1
source

You can get it from the AppRelativeVirtualPath property .

-1
source

Source: https://habr.com/ru/post/1724530/


All Articles