Not a subscriber?

Join thousands of others who are building self-directed lives through creativity, grit, and digital strategy—breaking free from the 9–5.
Receive one free message a week

ASP.NET File Name at Runtime

Retrieving the ASP.NET filename can be done in many ways. I’ve created this method in a few helper classes in some of the different jobs I have.

public string GetCurrentPageFileName()
{
   return new System.IO.FileInfo(Request.PhysicalPath).Name;
}

I’ve also created a base page class in the past to hold these types of methods (below).

/// <summary>
/// Base Page class
/// </summary>
public abstract class BasePage : System.Web.UI.Page
{
    protected string GetCurrentPageFileName()
    {
        return new System.IO.FileInfo(Request.PhysicalPath).Name;
    }
}

Throw this into the App_Code folder and change your default page implementation from System.Web.UI.Page to BasePage. You’ll have all the same methods you did before, but this time with the extra methods. Add more functionality into the BasePage as necessary.