May be not a usual need, but just thought about it… find the page name from the URL. The url might have query string or other things, but just want the page name like AboutUs.aspx from http://mysiteURL/AboutUs.aspx.
First, I created a RelativeURL method that takes the tilde (~) version and returns the absolute url from it. 2.0 framework introduced this cool tilde feature, but some times we need to find in script; so this method:
public static string RelativeURL(string relativeURL)
{
Image imgTemp = new Image();
imgTemp.ImageUrl = relativeURL;
return imgTemp.ResolveClientUrl(imgTemp.ImageUrl);
}
Well, a simple trick did that. Now using this method, we can grab just page name from any url, like:
public static string PageName
{
get
{
return RelativeURL("~" +
HttpContext.Current.Request.ServerVariables["URL"].ToString());
}
}
Love coding!
3 comments:
what about something like
using System.IO;
Path.GetFileNameWithoutExtension(Page.Request.PhysicalPath)
or even
Path.GetFileName(Page.Request.PhysicalPath)
That's cool. Glad to know an even better way to find it.
How to check from which page the current page is called. Like if write Respose.Redriect("2.aspx") in 1.aspx we want to know on 2.aspx page load event it is redirected from 1.aspx
Post a Comment