views:

1806

answers:

5

I have a legacy (haha) ASP.Net Webforms Web Site Project in Visual Studio 2008 SP1, that I would like to gradually introduce some MVC functionality into.

Most of the information I can locate on how to integrate ASP.Net MVC with WebForms seems to assume the use of a Web Application Project. However, it seems impossible to find information about how to retrofit an existing ASP.net Web Site Project with the ASP.Net MVC features.

I've reviewed Scott Hanselman's post and Chapter 13 of his upcoming book, both of which assume the Web Application Project type.

Is this possible? Does anyone have a how-to on this?

A: 

From what I understand, your question would be like Harley Davidson asking "we want to get in the car business, but don't want to start by creating a whole car, can we just start putting some car parts on our Fat Boys?"

I don't think you can really "sprinkle it in" to an existing project, although you could set up a separate project for just those views for which you'd like to use MVC.

For example, let's say you created a shopping portal, but wanted to use MVC to an extent. It would not be difficult to set up a separate shopping cart engine that was MVC-based and tapped into the underlying business logic DLLs of your current project. But the two would be completely separate solutions.

Jess
1) Nothing is too difficult with the right design. 2) MVC can be sprinkled into web forms (classis ASP.net applications easily)
Kyle LeNeau
+1  A: 

As long as you setup the routing in web.config, setup the necessary directory structure, and add the correct routes in global.asax, you could theoretically add MVC items to any web project. So far as I know, those are the only requirements for it to work.

However, the combination of the two might be a bit confusing and difficult to maintain, long term. Maybe you could move all of the existing web forms site content into a subfolder to keep it out of the way and keep the root directory of the site clean to reduce the clutter and make things more clear.

Chris
+10  A: 

Well for starters adding MVC to a webforms project is pretty simple, to get the features in VS 2008 for MVC takes a little bit more work (still easy). First you want to be sure you reference the assemblies and are using .Net 3.5. Second you can create a controllers folder and views folder in your current web forms project. You can also create a simple controller with an index action. Then setup/configure the routes in the global.ascx file. You should be set from there. Check here for reference.

However you will only be able to create aspx pages with bode behinds (you can delete those and enter the right inheritance class in the markup). To actually "convert" your project type so that you get the goodness of MVC and visual studio (add new view, goto controller, etc) is going to take some playing around with. My best advice is to create a new MVC project in VS 2008 and a new Web App project and compare the .csproj files in plain text. There is a long string value that tells VS the project template.

Believe me this does work. I have done it before on my own legacy projects. I don't remember how I found the project type "key" besides trial/error/elimination. ASP.Net MVC does play nice in the same project as webforms.

UPDATE: I think you can change to an MVC project type, which is still a web application by using these in the PropertyGroup of the .csproj file. Compare those to what you have and change the one that are differnt, be sure to copy/backup the file.

<ProjectGuid>{B99EC98A-1F09-4245-B00D-5AF985190AA9}</ProjectGuid>
<ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Update 2: You wouldn't affect your project or impact it very much. If you are un easy about it make a backup and play around. If you encounter changes you will always have the backup. I was skeptical at first but was glad I went down the MVC path.

Kyle LeNeau
Yeah, Ideally you're right; you probably do want to just convert over to a Web Application project. However, I don't want to have a large impact on an existing source tree, but would like to slowly introduce a few MVC features.
Kris
Changing the ProjectGuid and ProjectTypeGuid properties worked for me - it's really nice to have the extra MVC specific context menus (Add View, Goto View, etc) when developing. Thanks!
wows
Awesome, I am glad that worked for you.
Kyle LeNeau
This doesn't answer the question. the question specifically asked about doing a website project rather than a web app. i think the other answer is more relevant to the question asked.
Tom B
I've been wondering how to do this for weeks...great answer.
brad
This is a great answer, except that it has nothing to do with the question. This is about a Web Site not a Web Project so there is no .csproj to edit.
Nestor
+4  A: 

For a WebSite project, you just need to add Controllers to App_Code, not the root. You'll miss some VS goodness - as it doesn't know you're using MVC without the csproj file, but you'll actually get it working.

Just remember to inherit from Controller and ViewPage and you should be good.

Mark Brackett
A: 

Something that I learn, while trying to migrate an MVC2 application is that your project needs a Default.aspx. I was tasked with adding some GUI functions to an existing Web Services project, and therefore there was no default.aspx. Took me a while to figure out why my routes weren't being setup.

Chris Kemp