Better way to get Page Name

The way I interpret the question, what you’re looking for is an efficient way of retrieving the name of the currently executing aspx page, i.e. System.Web.UI.Page.

If that is true you shouldn’t have to deal with any FileInfo objects or hit the filesystem. Simply use the AppRelativeVirtualPath property on the page object:

using System;
using System.IO;
using System.Web.UI;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pageName = Path.GetFileNameWithoutExtension(Page.AppRelativeVirtualPath);
        }
    }
}

If you want to get the fully-qualified (or “rooted”) path of your currently executing page you can use Server.MapPath like this:

string path = Server.MapPath(Page.AppRelativeVirtualPath);

Using AppRelativeVirtualPath has the benefit of working even when you’re using URL rewriting and, since it doesn’t use Request.Url (which is provided by your users), you don’t have to worry about potentially malicious data.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)