Reputation: 33
I have a navigation bar that I have inside a bulletedList. My intention in doing that is to be able to change the classes based on where the current page sits on the site. I have code that can find the current filename, but I want to be able to retrieve the name of the folder that the current file is located. How do I do this?
protected void Page_Load(object sender, EventArgs e)
{
string[] file = Request.CurrentExecutionFilePath.Split('/');
string fileName = file[file.Length-1];
if (fileName == "Dashboard.aspx")
{
MainNavList.Items.FindByText("Dashboard").Attributes.Add("class", "active");
}
}
Upvotes: 1
Views: 5095
Reputation: 33
Thanks Yeronimo! All I did was change -1 to -2 and put in the name of my folder called "Dashboard". This is what worked for me:
protected void Page_Load(object sender, EventArgs e)
{
string[] file = Request.CurrentExecutionFilePath.Split('/');
string fileName = file[file.Length-2];
if (fileName == "Dashboard")
{
MainNavList.Items.FindByText("Dashboard").Attributes.Add("class", "active");
}
}
Upvotes: 2
Reputation: 48
Try using Request.MapPath
or Request.PhysicalPath
to get location of ASPX file on disk.
Upvotes: 0
Reputation:
With HttpContext.Current.Request.Url.AbsolutePath
you can get the current URL path.
As example if the page I was visiting was:
http://www.website.com/Directory1/Directory2/Page.aspx
Then it would output the string which you could use split()
:
/Directory1/Directory2/Page.aspx
Upvotes: 2