views:

476

answers:

3

I'm in the process of adding ASP.NET MVC to a WebForms app. One of the neat features of an ASP.NET MVC solution that you create from scratch is that a right click on the solution explorer and Add > New Item... will give a list of templates which include the MVC templates. However, in a WebForms app these templates don't appear.

I thought that there might be a setting in the .sln solution file that indicated that one was an ASP.NET MVC app but I couldn't find anything that that stuck out.

EDIT: To expand the question, how does Visual Studio know to add a "Controller..." menu item on to the "Add" menu when you right click on the Controllers folder in the Solution Explorer of an MVC app? Likewise it adds a "View..." menu item to the "Add" menu when you right click on the Views folder. Where does Visual Studio get this info from and how can I add those 2 folders to another web app and get the same functionality?

Ideas?

A: 

How about the other way around. Create an asp.net mvc project firstly, and then add your web form code over.

Here is an example of hybrid app by Scott Hanselman.

You may also consider to create one default asp.net mvc application and asp.net web form application, and then do a text compare of two projects to see what is the difference.

J.W.
+1  A: 

I believe that the logic for that is contained in the dll

Microsoft.VisualStudio.Web.ExtensionsUI

Which is registered in the Project template used when you create a new ASP.NET MVC project:

<WizardExtension>
  <Assembly>
    Microsoft.VisualStudio.Web.Extensions, 
    Version=9.0.0.0, 
    Culture=neutral, 
    PublicKeyToken=31bf3856ad364e35
  </Assembly>
  <FullClassName>
    Microsoft.VisualStudio.Web.Mvc.TemplateWizard
  </FullClassName>
</WizardExtension>

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Web\1033\MvcWebApplicationProjectTemplatev1.cs.zip

You could probably also mess around with the Project Type Guids in your .xxproj file:

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

Seem to be the default ones for an ASP.NET MVC project, compare those with the ones in your web application, and go from there.

However, in a lot of these circumstances I'd agree with gisresearch: it's often easier to create the more complex project (the MVC one) first, and then move the origininal into it.

Also, there's nothing to stop you having multiple projects hosted in the same web application - pull the common logic from the web application into a shared class library, and then reference that from a clean MVC app, with a slightly different namespace, and then merge the two sites within IIS, drop the dlls into a shared bin folder (or strongly name them and put them in the global assembly cache), share the images/scripts, and away you go - you just need to take care with the Global.asax, which would be common to both.

Zhaph - Ben Duguid
A: 

Like Zhaph mentioned, adding the ProjectTypeGuid worked for me. Unload the web project, edit, and add in the extra ProjectTypeGuid to the existing list.

{603c0e0b-db56-11dc-be95-000d561079b0};

Matt Davis