How To: Adding MVC items to an ASP.NET Web Application

May 20, 2009 • Damien White

Code

I’ve been experimenting with using ASP.NET MVC inside a standard ASP.NET Web Application (Web Forms).  I really like what’s been released with MVC 1.0 and can see places where existing apps would benefit from the features.  Plus, having just finished Professional ASP.NET MVC 1.0 from Wrox (see my book review), I couldn’t wait to start using it.

The last chapter of the book, Chapter 12, entitled “Best of Both Worlds: Web Forms and MVC Together,” discusses configuring an ASP.NET Web Application to support the new ASP.NET MVC features.  Note that adding Web Forms to an ASP.NET MVC project template is much easier (meaning no configuration/reference changes), since ASP.NET MVC is built on top of the ASP.NET Framework.  Anyway, after referencing the required libraries, creating the necessary directories, and updating the web.config, you’ll find that templates for the MVC items (e.g. Controllers, Views), are missing as options.

Well after digging around on the web, I found a solution to the problem.  It involves editing the project file and adding a guid to the ProjectTypeGuids node.

Caution: Before you do this, make sure that you have ASP.NET MVC 1.0 installed on your machine, also BACKUP your project file before modifying it.  If there is a mistake in the file, Visual Studio won’t load the project properly.

Now that you’ve made a backup, open up the project file using notepad (or your favorite text editor) and look for the ProjectTypeGuids node, for example (Note, line-breaks added for display in these examples, but in the project file everything is on a single line):

<ProjectTypeGuids>
    {349c5851-65df-11da-9384-00065b846f21};
    {fae04ec0-301f-11d3-bf4b-00c04f79efbc}
</ProjectTypeGuids>

In order to get the MVC items to work, you will add the project type guid of an ASP.NET MVC application.  The Guid that you need is {603c0e0b-db56-11dc-be95-000d561079b0};.  Simply add it to the beginning of the ProjectTypeGuids, for example (again, line-breaks added for display):

<ProjectTypeGuids>
    {603c0e0b-db56-11dc-be95-000d561079b0};
    {349c5851-65df-11da-9384-00065b846f21};
    {fae04ec0-301f-11d3-bf4b-00c04f79efbc}
</ProjectTypeGuids>

Save the project file and reload your project.  Now, things work correctly, for example when you right-click inside an action method, you see the option to add a View (Figure 1), and right-clicking on the Controllers folder gives you the option to add a new Controller (Figure 2).

Figure 01

Figure 1 – Adding a View

Figure 02

Figure 2 – Adding a Controller

Remember that by adding the ProjectTypeGuid for MVC, all the members of your team will need to have ASP.NET MVC installed, otherwise they will get errors as described in this post.

Posted in asp.net, how to, mvc, visual studio and tagged with ASP.NET, ASP.NET MVC

Damien White

I am a software architect with over 16 years of experience. I simply love coding! I have a driving passion for computers and software development, and a thirst for knowledge that just cannot be quenched. I'm happy to share what I know in my quest to learn as much as possible. I focus most of my time on web development using Ruby on Rails, Ember.js, and ASP.NET MVC.

comments powered by Disqus