user2059225
user2059225

Reputation: 33

Get the folder name of the current page ASP.NET C#

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

Answers (3)

user2059225
user2059225

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

Matija Bozicevic
Matija Bozicevic

Reputation: 48

Try using Request.MapPath or Request.PhysicalPath to get location of ASPX file on disk.

Upvotes: 0

user2742371
user2742371

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

Related Questions