Go Back

Handy trick for Master Pages in Visual Studio 2008 SP1

In case you hadn't noticed, there is a Beta of SP1 for Visual Studio 2008 and .NET Framework 3.5 out there. You should really check it out, it has tons of goodies, especially if you need some performance boosts in your WPF applications.

In this blog, I want to highlight a little hack that Microsoft added to the designer. In some instances, with complex nested Web Application Project solutions, the 2008 designer can not locate the master page. This used to be the case in 2005 designer too, but it would just show an error message for the master page portion of the form, and still show the content page. But in 2008, the content page is not displayed either, so you cant use the designer at all. I reported this to Microsoft and they were kind enough to provide a workaround. I just tested it out and it worked really well.

Our project has the following structure:

   /root (web application project)

   /root/module 1 (web application project)

   /root/module 2 (web application project)

   /root/module n (web application project)

   /root/lib projects (class library projects)

   /root/UI (web application project)

The master page resides in /root/UI.

It is referenced by content pages in all of the /module x projects, which are sibling directories to the UI directory, and all under the root web app directory. The master page is referenced "~/UI/xxx.Master". This is the reference that does not work in design time (as ~ is resolved to be the local module x web app root in design time, but in runtime it correctly resolves to the global app root).

So here is the work around:

  1. I made a copy of the master page and placed it in the root directory
  2. I renamed it __fallback.Master (this is a magic word, needs to be exactly this), also renaming the code behind and designer files too (__fallback.Master.cs and __fallback.Master.designer.cs)
  3. I included the copied master page in the root web application project (it didn't work without this step)
  4. I adjusted URLs in the copy to reflect its new location
  5. Now, it worked in design time! But not in run time, because there were two classes with the same name, so I edited the copied master page and changed the class name to FallBackXXX.master - you can choose any name, the class will never be used in runtime)

That did it! Of course, you could use any old master page for the fake __fallback page, even just a blank one, but if you want it to look like it should in design time, it is better to use a modified copy of your actual master page as detailed above.

Thanks Microsoft for the workaround!

  • Facebook
  • Twitter
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Add diigo bookmark

Comments  3

  • Kumar 05 Aug

    Hi John,
    Thanks for your informative post. I was wondering if I could ask you a very basic question related to your post. How did you go about creating those nested web app projects in Visual Studio 2008? I tried creating it and I am unable to figure out the settings I need to enforce for the root as well as nested web app project in the Web tab of the properties window for the project. Would it be possible for you to point me in the right direction? Thanks a lot once again.
  • John Waters 05 Aug

    Kumar, wow, that was a long time ago I blogged about that!
    Truth is that Visual Studio never quite 'understood' the structure of the project, and had a hard time showing the pages with master pages. It compiles and runs just fine, but the design time experience is not that good. There is certainly no built in support in the tool to create the projects. I basically just placed them in that heirarchy in the file system and then added them to the solution. The web.config goes in the topmost project, but can be overridden in subdirectories. That's about all I can recall. Thanks for asking!
  • Kumar 06 Aug

    Hi John,
    Thanks for the quick reply! Ya, I figured as much since I cannot find any documentation on support for nested web app projects in Visual Studio. I will give it a try and see how it goes. 
Post a comment!
  1. Formatting options