ASP.NET MVC: Discover the MasterPageFile Value at Runtime
A couple weeks ago it was finally time to add a context-sensitive, data driven menu system to our MVC application. As I thought about it I was stuck. I wasn't sure what the best way to implement it was. As is common with an MVC application there was no 1-to-1 relationship between actions and views. And even more difficult was that our *.master files could be used by views tied to different controllers. So it was looking like I would have to load the data I needed from the ViewMasterPage.
I really didn't like this option and looked around a bit trying to find out what others had done. Here's a couple examples of what I found:
- It's ok to break the pattern when needed
- Partial Requests
- Add the data to ViewData from the Action
While all of these options work, none of them sat well with me because they either require me to remember to include the data or they feel contrived or foreign to MVC.
When you create a new View file you can specify that you want to use a MasterPage. When you do this your @Page Directive will look like this:
This can be changed as needed but if you are using MasterPages in your application you the value of the MasterPageFile is exactly what you need to determine which MasterPage is being used by the view being returned. I like this idea because the same action can return different views, or even result in a redirect, so it isn’t until you actually arrive at the controller’s ActionExecuted event that you know for sure that the result is a View and which view that will be.
The key to the whole thing is you need to be able to read the @Page directive located in the first line of your ViewPage. When you’re handling the OnActionExecuted event you get an ActionExecutedContext object passed in from System.Web.MVC.ControllerBase which contains the result of Action which just finished executing. Here’s what you do to get from the start of the event to the value of MasterPageFile:
- Check to see if ActionExecutedContext.Result is an ViewResult
- Check to see if ViewResult.ViewName has been set (if you’re writing tests for your Actions you’ll be doing this anyway). If it hasn’t then you know that the name of your view will be the same as the Action, so you can get the value from ControllerContext.RouteData.
- As long as you are using the WebForms view engine (or inheriting from it) you can use the ViewResult.ViewEngineCollection.FindView method to let the ViewEngine find the view for you.
- FindView returns a ViewEngineResult has a View property which returns a WebFormView which in turn has a ViewPath property.
- At this point you can get the source of your view, parse it and retrieve the value of MasterPageFile. Once you’ve done this I’d recommend caching the value to prevent the need to parse the file every time.
Here’s what the full implementation looks like:
I’ve implemented this as an ActionFilterAttribute so you can just apply it to any controller or action. This way you can use it in a more flexible way. The only thing left for you to do is fill in the blanks in the LoadData method to retrieve the data you need based on the name of the MasterPageFile.
We’ve been running this setup for a couple weeks now in development, QA and UA and it’s working like a charm so far. Once you have it setup, you’re free to forget about it until you need to change how your menus function or your data set. Plus now you’re keeping all your interactions with your model inside your controller and your view just needs to pull the data from the ViewDataDictionary.
Labels: ASP.NET MVC