C# – Web Forms and MVC in the same web project

asp.netcmicrosoftmvcwebforms

I'm working in a large legacy project which was build with ASP.NET Web Forms and the idea to add in the MVC libraries to the same project has been tossed around. I know that this is possible (there are plenty of articles available), but I'm wondering if/why this is/is not recommended by anyone who has done something similar.

The idea is that we could build new areas of the application with the newer MVC pattern and keep the legacy pages running as they always have.

EDIT: To clarify, we are not considering the MVP pattern as that is already being used in some parts of this project but doing something similar to what is described in this article: https://www.simple-talk.com/dotnet/asp.net/mixing-web-forms-and-asp.net-mvc/

Best Answer

This isn't a bad way to go about rewriting an application. This would be particularly useful for a long term rewrite happening in phases:

  1. Add a "test project" that utilizes CodedUI tests (or Watin or Selenium) to test the WebForms application through the browser. You could also use SpecFlow to define your test cases. I've used this to great affect on a WebForms application.

  2. After you have good test coverage of the WebForms application, you can begin the next phase of refactoring: Moving common components into a separate class library. This is where your models and data access layer could be created. You can add a reference to this library to your WebForms applications. The tests you wrote in phase 1 make sure you don't break existing business rules

  3. Port functionality over to your MVC application, modifying your tests to use the MVC app not the WebForms app.

Steps 1 and 2 could be tackled simultaneously. You can even start on Step 3 once you have an entire feature tested, and its common components are decoupled from the WebForms application.

Split the work out into main features. Once a feature is supported in the MVC application, shut it down in the WebForms application. It will eventually become a hollowed out husk of its former self until you can remove the code all together.