How to get the page name of itself

Discussion in 'ASP .Net' started by ad, Jun 26, 2005.

  1. ad

    ad Guest

    When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    (MyPage.aspx)?
     
    ad, Jun 26, 2005
    #1
    1. Advertising

  2. You can find the name through the ServerVariables Collection. Use the C#
    example below to get the current page name.

    Request.ServerVariables["SCRIPT_NAME"].ToString()

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage

    "ad" <> wrote in message
    news:%...
    > When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    > (MyPage.aspx)?
    >
    >
     
    Mark Fitzpatrick, Jun 27, 2005
    #2
    1. Advertising

  3. A little bit more than that is needed.

    That will return the page name *if* it's in the root.

    If the page is in an application directory,
    it will return a path which includes the virtual directory

    You need to split the URL array to get just the page name.

    Here's a routine which will return the script's name
    no matter whether the page is in an application,
    in the root, or in a deeply nested subdirectory.

    script_name.aspx
    ------------------
    <%@ Page Language="VB" %>
    <html>
    <head>
    <title>Page Name</title>
    </head>
    <script runat="server">
    Public Sub Page_Load(Sender As Object, E As EventArgs)
    Dim strURL, arrayURL, pagename
    strURL = Request.ServerVariables("SCRIPT_NAME")
    arrayURL = Split(strURL, "/", -1, 1)
    pagename = arrayURL(ubound(arrayURL))
    script.text = pagename
    End Sub
    </script>
    <html>
    <body>
    <form id="Form1" runat="server">
    <p>
    <asp:Label id="script" runat="server" /><BR>
    </form>
    </body>
    </html>
    ------------




    Juan T. Llibre
    ASP.NET MVP
    http://asp.net.do/foros/
    Foros de ASP.NET en Español
    Ven, y hablemos de ASP.NET...
    ======================

    "Mark Fitzpatrick" <> wrote in message
    news:...
    > You can find the name through the ServerVariables Collection. Use the C# example below
    > to get the current page name.
    >
    > Request.ServerVariables["SCRIPT_NAME"].ToString()
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - FrontPage
    >
    > "ad" <> wrote in message
    > news:%...
    >> When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    >> (MyPage.aspx)?
    >>
    >>

    >
    >
     
    Juan T. Llibre, Jun 27, 2005
    #3
  4. ad

    ad Guest

    Thank!
    But the "Request.ServerVariables["SCRIPT_NAME"].ToString()" will get the
    whole path like:
    /AppPath/SubPath/MyPage.aspx. But I only want the page name.
    How can I do that?

    "Mark Fitzpatrick" <> ¼¶¼g©ó¶l¥ó·s»D
    :...
    > You can find the name through the ServerVariables Collection. Use the C#
    > example below to get the current page name.
    >
    > Request.ServerVariables["SCRIPT_NAME"].ToString()
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - FrontPage
    >
    > "ad" <> wrote in message
    > news:%...
    > > When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    > > (MyPage.aspx)?
    > >
    > >

    >
    >
     
    ad, Jun 27, 2005
    #4
  5. See my previous reply.



    Juan T. Llibre
    ASP.NET MVP
    http://asp.net.do/foros/
    Foros de ASP.NET en Español
    Ven, y hablemos de ASP.NET...
    ======================

    "ad" <> wrote in message news:u$...
    > Thank!
    > But the "Request.ServerVariables["SCRIPT_NAME"].ToString()" will get the
    > whole path like:
    > /AppPath/SubPath/MyPage.aspx. But I only want the page name.
    > How can I do that?
    >
    > "Mark Fitzpatrick" <> ¼¶¼g©ó¶l¥ó·s»D
    > :...
    >> You can find the name through the ServerVariables Collection. Use the C#
    >> example below to get the current page name.
    >>
    >> Request.ServerVariables["SCRIPT_NAME"].ToString()
    >>
    >> Hope this helps,
    >> Mark Fitzpatrick
    >> Microsoft MVP - FrontPage
    >>
    >> "ad" <> wrote in message
    >> news:%...
    >> > When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    >> > (MyPage.aspx)?
    >> >
    >> >

    >>
    >>

    >
    >
     
    Juan T. Llibre, Jun 27, 2005
    #5
  6. You can get it by only one row of code by using System.IO.Path:
    C#
    string pagename =
    System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"])

    "ad" wrote:

    > When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    > (MyPage.aspx)?
    >
    >
    >
     
    =?Utf-8?B?Sm91bmkgS2FycHBpbmVu?=, Jun 27, 2005
    #6
  7. Great shortcut, Jouni!
    Thanks for posting it.



    Juan T. Llibre
    ASP.NET MVP
    http://asp.net.do/foros/
    Foros de ASP.NET en Español
    Ven, y hablemos de ASP.NET...
    ======================

    "Jouni Karppinen" <> wrote in message
    news:...
    > You can get it by only one row of code by using System.IO.Path:
    > C#
    > string pagename =
    > System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"])
    >
    > "ad" wrote:
    >
    >> When I a webpage, say "MyPage.aspx", How to get the page name of itsef
    >> (MyPage.aspx)?
    >>
    >>
    >>
     
    Juan T. Llibre, Jun 27, 2005
    #7
  8. ad

    ezraeel

    Joined:
    Oct 6, 2009
    Messages:
    1
    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;
    }
     
    ezraeel, Oct 6, 2009
    #8
  9. ad

    macuto_dav

    Joined:
    Nov 25, 2010
    Messages:
    1
    Easy

    Response.Write(System.IO.Path.GetFileName(Page.Request.FilePath))
     
    macuto_dav, Nov 25, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page