简体   繁体   中英

How masterpage get the current page from masterpage by itself

I am writing a website with Visual Studio 2008, C# 3.5 and ASP.NET MVC 2. I put the navigation bar in the masterpage.But there is problem that I will not know which button is needed to be highlight(current page) in the navigation bar.

I want get the current page that need to be highlight by masterpage self (not through the content page).And I don't think it is a good way to get the current page by url string.Because I have no idea about the final url is.

So how can I do this?

I guess you could set a ViewData["currentPage"] value in the Action methods, this would allow you to process that ViewData in the Masterpage. That is, however, off the top of my head and I'm sure there's a more elegant way to do this.

When you click the link in the navigation bar (in the master page) that should be triggering a controller action that will decide which view content page will be shown. In that action method you can (as Lazarus suggests) add some data identifying the current page to ViewData that will be picked up by the master page to modify the navigation bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM