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?
The Microsoft .NET 4.0 exam on Web development (70-519) has almost this exact question in the prep materials. The answer, according to Microsoft then, is:
This info is in paid-for materials my employer purchased so there is not necessarily a web page stating it this clearly to which I could link.
I had a pretty big ASP.NET web site (not a web application) and wanted to add MVC3 to it. I had no option to change the type of the project so I had to go with the web site (asp.net 4.0).
I use a separate MVC project but not as it's own web application but as an assembly within my old web site.
Here's a rundown of what I did:
Added Routing to the web site. I just copied over some code from an MVC global.asax in the global.asax of my web site. We need some usings:
In Application_Start we need:
Then add the usual routing methods:
Next add a few things to the web.config of your web site. In system.web under compilation we need the following assemblies:
Initially I also added some MVC namespace to web.config but it seems to work fine without them.
You do now create new routes in Global.asax of the web site, then add a corresponding controller in the MVC project and then back to the web site to add a view for it. So you logic is all in a assembly while the views and the routing is defined in the web site.
You can still debug into the MVC controllers by setting break points there, but you debug by starting the web site.
If you use the suggested default route of MVC:
a call to www.mysite.com will serve up the content of the home controller/view not your old default.aspx home page, so I just don't use such a route. If you routes conflict with existing physical folders and files, use constraints with regular expressions on the routes to exclude such conflicts.
Even though I use master pages in the web site, the actual html for the common page parts is created by code in another assembly. I could just call the same methods from my _ViewStart.cshtml or my base controller.
So far I have not seen any real negative about this approach.