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?
From stackoverflow
-
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)
CoffeeAddict : I am trying to get it from the page object, not the request object.Larsenal : You can't rely on getting it from the page object. Especially with URL routing becoming common, the page URL (PageName.aspx) may not always map to a page class named PageName. Like it or not, the correct way to get the "page name" used in the URL is to use the Request object. One of the properties of System.Web.UI.Page is Context. You say you don't want to worry about Context. However, a Page object is always going to have access to Context through that property. I think you need to help us understand why you're so adamant about using something other than Context. -
inside the page code,
this.GetType().Nameor
this.GetType().Name.Split('_')[0]Hogan : this.GetType().Name.Replace("_",".")moomi : Hogan is right!Hogan : only sometimes.CoffeeAddict : what's wrong with this.Page? -
You can get it from the AppRelativeVirtualPath property.
Hogan : This is a path and not a pagenameStuart Dunkeld : So extract the page name from the path, what's the issue?Hogan : http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.apprelativevirtualpath.aspx The path does not include the page name or file name.Hogan : actually the documentation is not 100% clear. -
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.
Stuart Dunkeld : OP doesn't want to use Context.
0 comments:
Post a Comment