Search code examples
c#asp.netmaster-pages

How do I get the name of the request's content file from a master page?


In my SiteMaster.cs file I have the following snippet:

protected void Page_Load(object sender, EventArgs e)
{
    pageName.Text = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
}

The idea with this is on my SiteMaster I can display the current file name by using:

<asp:Label ID="pageName" runat="server" />

However, I'd like to use pageName in an if statement. For example, like this

if (pageName == 'default.aspx') 
Then  Response.Write ("test") 
End if //pseudo code

Is this possible or am I doing it the wrong way?


Solution

  • Use this for retrieving Current page name from master page like

    String pageName  = Request.FilePath;
    
    //return like that /example.aspx
    
    if (pageName == "/default.aspx") 
    {
    // your code
    }