ASP.NET MVC: Remember The Config

.aspx.cs and .aspx.designer.cs

These files are not needed in asp.net mvc views, but when you add the default mvc pages supplied with the mvc install, you will get them by default.  I decided to play around with removing them completely and remembered that there are some things to consider when adding views to your asp.net mvc application(and this also applies to "old school" webforms style apps).

@Page Directive and Web.Config

Most, if not all of the attributes of the @Page directive can be set sitewide in the web.config via the pages element and the compilation element, eliminating the need for the @Page directive altogether.  The important attributes are Language, MasterPageFile, and Inherits.

Weird Errors

Removing the @Page directive however causes some weird errors to appear in the errors list. They appear to be indicating some VB.NET type errors(note the End Namespace reference below). They don't affect anything, they are just annoying.

Beware: Master Pages Everywhere

This is one to think about, maybe.  Everytime you add a new mvc view content page, you have to pick a master page.  If you did this on every single view content page and then decide you want to use a different master page, you would have to go to each view content page and change the name of the master page file.  Unless of course if you decide to just replace the contents of one master page.

Sometimes you want the ability to test different master pages concurrently.  Perhaps you are coming up with several site designs you need to show your customer to let them pick which one looks best.  This requires easily switching master pages files.  You can do this in the web.config in the pages element. After specifiying a base page for all view pages and the master page file, the element would look something like this:


  
    
    
    
    
    
    
  

1
2
3
4
5
6
7
8
9
10
11
12

Really Stupid Example

In my example for this post, one of the layouts looks like this:

And the other layout looks like this:

The point is that none of the content pages needed to be touched, just the web config, to display completely different layouts.

Here's the source code for the example(based on Mvc Preview 5) for this post.

 
Author: , 0000-00-00