ASP.NET Get the Current Web Page Name

ASP.NET

ASP.NET

It’s a common requirement when developing Web applications to need to programmatically find out the name of the running Web page.

In this article I will look into some of the basic commands in ASP.NET that you can use to find the name of the Web page that is currently running the script. As part of this process I will also cover some of the typical component parts of a URL that you may have to deal with.

For clarity I’ll review the same commands in both C# and VB.NET.

In both VB.NET and C# you can find the current page name using:

  • System.IO.Path.GetFileName
  • System.IO.FileInfo
  • System.Web.VirtualPathUtility.GetFileName

If you find this article useful, check out some other ASP.NET tips I have written.

C#

In C# you can use the following syntax variants to find the file name of the current Web page. I have clustered the variants into groups and have color coded the parameters that each command group will accept.

System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]);
System.IO.Path.GetFileName(Request.CurrentExecutionFilePath);
System.IO.Path.GetFileName(Request.Url.ToString());
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath);

(new System.IO.FileInfo(Request.ServerVariables["SCRIPT_NAME"])).Name;
(new System.IO.FileInfo(Request.CurrentExecutionFilePath)).Name;
(new System.IO.FileInfo(System.Web.HttpContext.Current.Request.Url.AbsolutePath)).Name;

System.Web.VirtualPathUtility.GetFileName(Request.ServerVariables["SCRIPT_NAME"]);
System.Web.VirtualPathUtility.GetFileName(Request.CurrentExecutionFilePath);
System.Web.VirtualPathUtility.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath);

VB.NET

Here is the VB.NET syntax for the same commands listed in C# above. These will all work to return the file name of the current Web page. As with the C# examples I have grouped the commands and have color-coded the parameters they take.

System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
System.IO.Path.GetFileName(Request.CurrentExecutionFilePath)
System.IO.Path.GetFileName(Request.Url.ToString())
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)

(New System.IO.FileInfo(Request.ServerVariables("SCRIPT_NAME")).Name
(New System.IO.FileInfo(Request.CurrentExecutionFilePath)).Name
(New System.IO.FileInfo(System.Web.HttpContext.Current.Request.Url.AbsolutePath)).Name

System.Web.VirtualPathUtility.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
System.Web.VirtualPathUtility.GetFileName(Request.CurrentExecutionFilePath)
System.Web.VirtualPathUtility.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)

So now that we know how to get the full file name, how about getting either the name or the extension?

If you just want the name and not the extension:

System.IO.Path.GetFileNameWithoutExtension(Request.Url.AbsolutePath);

If you want the extension use:

System.IO.Path.GetExtension(Request.Url.AbsolutePath);

This will give you with a .NET page the extension: .aspx

How to get the Parameters of a URL

Often it’s important to be able to parse out any extra parameters that were sent as part of the URL. Here’s how we can get them as a string:

Request.QueryString.ToString();

This will return the parameters: var1=cats&var2=test
From the full URL:

http://TestWebSite/CSharpPageName.aspx?var1=cats&var2=test

How to get the Root URL

To get the root URL (ie: http://example.com) you can use the following:

Server.MapPath(".")

Or

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s