How to get current page Name in ASP.NET

simple but nice to know

How to get current page Name in ASP.NET

Sometimes you need the current page name of aspx page in asp.net. It’s natural to search something like  

 Page.Name  // but this is not avaliable//

When I was develeoping my personal website then I need this one. Then I googled and but not found the solution and  then made quick logic like followings  

        string pageName = Page.ToString();
        pageName = pageName.Replace("_aspx", "");
        pageName = pageName.Replace("ASP.", "");
        int start = 0;
        if (pageName.IndexOf("_") != -1)
        {
            start = pageName.IndexOf("_") + 1;
        }
        pageName = pageName.Substring(start);

Looks Weird …..Right?
After uploading the second version of my personal website with above code I start googling again and found the proper solution

string currentPageName = Path.GetFileNameWithoutExtension(Request.PhysicalPath);
 

I just want share this simple topic you may already know… but it might help the people who don’t know this…and going to write some weird code!!

Tags:geek tech asp.net 
Comments(0) Posted By saiket | 9/3/2009 12:00:00 AM