PositiveGuy
PositiveGuy

Reputation: 47743

Get PageName.aspx from Page object

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

I guess page already has the name and I need to just append .aspx but is there a way to get the extension with it automatically?

Upvotes: 3

Views: 21277

Answers (5)

Virag Dilip Desai
Virag Dilip Desai

Reputation: 159

May be little too late but this would help someone else..

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

Upvotes: 0

Hogan
Hogan

Reputation: 70513

I believe this will work (but I did not test)

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

It is basically the same as Larsenal but it uses a static parser and not an object.

Upvotes: 2

stuartd
stuartd

Reputation: 73243

You can get it from the AppRelativeVirtualPath property.

Upvotes: -1

moomi
moomi

Reputation: 151

inside the page code,

this.GetType().Name

or

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

Upvotes: 0

Larsenal
Larsenal

Reputation: 51146

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)

Upvotes: 7

Related Questions