2013-04-18 115 views
2

我有一个导航栏,里面有一个bulletedList。我这样做的目的是能够根据当前页面在网站上的位置来改变类别。我有可以找到当前文件名的代码,但我希望能够检索当前文件所在的文件夹的名称。我该怎么做呢?获取当前页面的文件夹名称ASP.NET C#

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"); 
    } 
} 
+2

字符串文件名=文件[file.Length - 2]。会给你的文件夹之前的文件夹?不知道如果您为根页面中的页面执行此操作会发生什么情况。可能要测试如果file.Length> 1 – Yeronimo

+0

**好!**谢谢!您的解决方案非常棒! – user2059225

回答

1

谢谢Yeronimo!我所做的只是将-1改为-2,并放入名为“Dashboard”的文件夹中。这是对我工作:

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"); 
    } 

}

2

随着HttpContext.Current.Request.Url.AbsolutePath你可以得到当前的URL路径。

作为例子,如果我访问的页面是:

http://www.website.com/Directory1/Directory2/Page.aspx

然后,它输出的字符串,你可以使用split()

/Directory1/Directory2/Page.aspx

0

尝试使用Request.MapPathRequest.PhysicalPath到获取ASPX文件在磁盘上的位置。