Migrating ASP.NET Web Forms app to .NET Core using DotVVM
DotVVM is an open source MVVM framework for web applications that supports ASP.NET and ASP.NET Core.
There are still plenty of old Web Forms applications out in the world. They are difficult to maintain and extend. The authors of such apps stand in front of a difficult choice:
- to rewrite the entire application from scratch using a modern stack
- to start a long-running modernization process and do it "on the fly" using small, incremental upgrades
The total rewrite is a timely process. It requires a huge effort, it is often hard to get approved by company management, and requires the developers to adopt a lot of new knowledge. Morover, it is difficult for the team to add new features in the application as they have a lot of work with the rewrite.
About the sample
This sample demonstrates how DotVVM can be used in the process of incremental modernization of Web Forms applications. Every step of the process can be seen by checking out one of the repository branches.
We recommend to install an extension for Visual Studio that brings the IntelliSense and templates for DotVVM files.
Step 1: The
01_webforms branch contains a simple Web Forms application with a few pages and a simple usage of the
Step 2: The
02_install_dotvvm branch shows how to install DotVVM in the project.
- Add the OWIN Startup Class Startup.cs.
- Add the DotVVM Configuration file DotvvmStartup.cs.
- Add the DotVVM project type GUID (
94EE71E2-EE2A-480B-8704-AF46D2E58D94) as a first one in the
<ProjectTypeGuids>element in OldWebApp.csproj to make the DotVVM extension in Visual Studio work properly.
Now you can create new pages in the application using DotVVM, and start an incremental upgrade of the ASPX pages to DotVVM. See the differences cheat-sheet to learn about the differences between DotVVM and ASP.NET Web Forms.
Step 3: The
03_migration_in_progress branch shows some pages migrated to DotVVM while the rest remains in ASP.NET Web Forms.
- A master page Site.dotmaster was created. Its content was copied with a few changes made:
asp:ContentPlaceHolderwas changed to
dot:ContentPlaceHolder, all the
runat="server"were removed and so on.
- A viewmodel SiteViewModel.cs with an abstract property
Titlewas added in the project.
- A resources for jQuery and Bootstrap were configured in DotvvmStartup.cs.
- The pages About.dothtml and Contact.dothtml were migrated in DotVVM.
- The route registrations for the migrated pages were moved from Global.asax.cs to DotvvmStartup.cs.
The deployment model of the application remains the same - you don't need to change anything. This stage can take a long time as there may be hundreds of pages in the application.
During the migration, you can make another refactorings, and thanks to the MVVM approach of DotVVM, your viewmodels will be testable. This can improve the further maintainability of the application.
Step 4: The
04_migration_complete branch shows all the pages migrated to DotVVM.
- The Default.dothtml was migrated and demonstrates the GridView and DataPager controls in DotVVM.
- The DefaultViewModel.cs demonstrates the MVVM approach for data-binding and the usage of
Now the application doesn't use any of the ASP.NET Web Forms features, so it's ready to be switched to .NET Core. In real-world applications, there might be some other things to change like authentication (needs to be migrated to ASP.NET Core packages), bundling and more. All these things will need to be addressed during the next step.
Step 5: The
05_dotnetcore branch shows the changes made to the project file in order to switch the project to .NET Core 2.2.
- The OldWebApp.csproj was converted to the new format and the
Dotvvm.Owinpackage was replaced by
- The scripts and CSS files were moved to the
- The Startup.cs was updated to use the ASP.NET Core
Switching to .NET Core may require additional configuration steps on the server to be performed. Make sure you plan this process in advance so the upgrade process is smooth.
As you can see, the migration is not easy - there is no magic wizard that will just "convert" the application from ASP.NET Web Forms. However, this method allows to migrate the application while new features can be added continuously, and means much less effort than a total rewrite. You can also learn DotVVM on the fly, start with simple pages and continue with the complex ones.