Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to get the page name of itself

Reply
Thread Tools

How to get the page name of itself

 
 
ad
Guest
Posts: n/a
 
      06-26-2005
When I a webpage, say "MyPage.aspx", How to get the page name of itsef
(MyPage.aspx)?


 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      06-26-2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> When I a webpage, say "MyPage.aspx", How to get the page name of itsef
> (MyPage.aspx)?
>
>



 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      06-27-2005
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隳l
Ven, y hablemos de ASP.NET...
======================

"Mark Fitzpatrick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> When I a webpage, say "MyPage.aspx", How to get the page name of itsef
>> (MyPage.aspx)?
>>
>>

>
>



 
Reply With Quote
 
ad
Guest
Posts: n/a
 
      06-27-2005
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" <(E-Mail Removed)> 撰寫於郵件新聞
:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > When I a webpage, say "MyPage.aspx", How to get the page name of itsef
> > (MyPage.aspx)?
> >
> >

>
>



 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      06-27-2005
See my previous reply.



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

"ad" <(E-Mail Removed)> wrote in message news:u$(E-Mail Removed)...
> 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" <(E-Mail Removed)> 撰寫於郵件新聞
> :(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>> > When I a webpage, say "MyPage.aspx", How to get the page name of itsef
>> > (MyPage.aspx)?
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
=?Utf-8?B?Sm91bmkgS2FycHBpbmVu?=
Guest
Posts: n/a
 
      06-27-2005
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)?
>
>
>

 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      06-27-2005
Great shortcut, Jouni!
Thanks for posting it.



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

"Jouni Karppinen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)?
>>
>>
>>



 
Reply With Quote
 
ezraeel ezraeel is offline
Junior Member
Join Date: Oct 2009
Posts: 1
 
      10-06-2009
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.Absolut ePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
 
Reply With Quote
 
macuto_dav macuto_dav is offline
Junior Member
Join Date: Nov 2010
Posts: 1
 
      11-25-2010
Response.Write(System.IO.Path.GetFileName(Page.Req uest.FilePath))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
obtain element name, or attribute and value of the document name itself, and some elemnts and attributes from an ancestor or the node itself using xquery Jeff Kish XML 4 10-30-2008 05:47 PM
How to get the page name of itself ad ASP .Net 3 06-18-2006 12:05 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
How to get the name of a running asp.net application from the application itself??? Bob Rock ASP .Net 2 06-14-2004 06:44 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments