Get current page name

This function can be used to retrieve the current page:s name, i.e default.aspx, hello.aspx or whatever.

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

By going through System.Web.HttpContext.Current object we are able to have this function in a generic dll or class – and not in each and every page needing to call it.

2 Responses to “Get current page name”

  1. Jeff says:

    I tried this and got the following Argument Exception when creating the FileInfo object.

    URI formats are not supported.

  2. Otto says:

    Hi,

    Thanks for your post. It helped me get what I was looking for =D

Leave a Response