我在 bulletedList 中有一个导航栏。我这样做的目的是能够根据当前页面在站点上的位置更改类。我有可以找到当前文件名的代码,但我希望能够检索当前文件所在文件夹的名称。我该怎么做?
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");
}
}
最佳答案
使用 HttpContext.Current.Request.Url.AbsolutePath
你可以获得当前的 URL 路径。
例如,如果我访问的页面是:
http://www.website.com/Directory1/Directory2/Page.aspx
然后它会输出你可以使用split()
的字符串:
/Directory1/Directory2/Page.aspx
关于c# - 获取当前页面的文件夹名称 ASP.NET C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16082113/