George English

Wednesday, March 11, 2009

Get Current Page Name Function

I needed to get the page name from a URL recently. Here's a very simple function that you can put in a common class for use throughout your site.

VB

Public Shared Function GetCurrentPageName() As String
Dim sPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
Dim oInfo As New System.IO.FileInfo(sPath)
Dim sRet As String = oInfo.Name
Return sRet
End Function


C#

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

Labels: