I had a rather large ASP.NET website (and not a web application) and I wanted to add MVC3 to it. I did not have the opportunity to change the type of project, so I had to go with a website (asp.net 4.0).
I am using a separate MVC project, but not as my own web application, but as an assembly on my old website.
Here is a summary of what I did:
In Application_Start, we need:
AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes);
Then add the usual routing methods:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Then add a few things to the web.config of your website. In system.web, when compiling, we need the following assemblies:
<assemblies> <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies>
Initially, I also added some MVC namespace to web.config, but it seems to work just fine without them.
Now you create new routes in the Global.asax website, then add the appropriate controller to the MVC project, and then return to the website to add a view for it. So, you are all logical in the assembly, while the views and routing are defined on the website.
You can still debug MVC controllers by setting breakpoints there, but you are debugging by running the website.
If you are using the suggested default MVC route:
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
a call to www.mysite.com will serve the contents of the home controller / view, not your old default.aspx, so I just don't use that route. If you are redirecting a conflict with existing physical folders and files, use regex constraints with routes to prevent such conflicts.
Although I use the master pages on the website, the actual html for the common parts of the page is generated 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 consequences of this approach.