How to get the page name of itself

M

Mark Fitzpatrick

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
 
J

Juan T. Llibre

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>
------------






Mark Fitzpatrick said:
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 said:
When I a webpage, say "MyPage.aspx", How to get the page name of itsef
(MyPage.aspx)?
 
A

ad

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 said:
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 said:
When I a webpage, say "MyPage.aspx", How to get the page name of itsef
(MyPage.aspx)?
 
J

Juan T. Llibre

See my previous reply.





ad said:
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 said:
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 said:
When I a webpage, say "MyPage.aspx", How to get the page name of itsef
(MyPage.aspx)?
 
G

Guest

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"])
 
J

Juan T. Llibre

Great shortcut, Jouni!
Thanks for posting it.





Jouni Karppinen said:
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 said:
When I a webpage, say "MyPage.aspx", How to get the page name of itsef
(MyPage.aspx)?
 
Joined
Oct 6, 2009
Messages
1
Reaction score
0
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top