Skip to main content

WebForms To MVC

Bill Watts - October 30, 2021

I just finished upgrading a website from VB.Net WebForms to .NET 5, the latest version of .Net from Microsoft. It was a fun coding challenge, with several benefits to my client. The functionality and the UI didn't change much for the end-user, but the underlying codebase is significantly better.

In 2009, I created this VB.Net WebForms application for a Cincinnati-based construction company, which they use to manage their construction projects. They depend on this software for running their business, and have no plans to switch to a different program.

This upgrade will be valuable to my client for several reasons:

  1. Development can be done faster and more efficiently.
  2. Support is easier to find.
  3. Security updates, performance streamlining, and third-party integrations are available.
  4. Cloud hosting is standard.
  5. Plus, I still make code changes when the client requests enhancements, and I didn't want to write VB.Net anymore.

Fortunately, the architecture of the software was already pretty good, for a monolithic application. I had written object-oriented code with N-Tier architecture, so the business logic was separate from the UI. The UI layer was VB.Net, but all the underlying layers were C#.Net.

First, I began changing the VB.Net WebForms (aspx) into MVC (vbhtml), with true Views and Controllers. I was able to have both aspx and vbhtml files in the same Web project, with a single user session, so I could make this change gradually and simultaneously view both versions of a webpage. Major changes were needed in each aspx file, since I had used many of the .Net controls, such as asp:Textbox, asp:DropDownList, asp:GridView, etc. I converted all the .Net controls to html elements in the vbhtml. This was a huge rewrite, but it felt great to retire the .Net controls. I was able to maintain the old aspx URLs via route config.

One thing I learned during this process was to fix the code before doing the upgrade. Some of the shortcuts or bad practices that were possible in WebForms, such as embedding business logic in the aspx, aren't easily transitioned to newer versions of .Net. Instead, re-writing the WebForms to use best practices, and then upgrading to MVC, was much easier.

Next, when all of the aspx files were gone, I could switch to .NET 5. This required a change from VB to C#, but I had used minimal code in the vbhtml and controller functions, so this wasn't a huge change. I was able to use a code convertor for the controller functions. I manually changed the vbhtml to cshtml. I created static If() and FormatCurrency() functions in C#, since I had used those frequently in the vbhtml.

As part of this step, I replaced Windows Authentication with a Login webpage. I also switched Entity Framework from db-first to code-first. Reviewing this old code felt like a trip down memory lane, to a time when we used things such as WindowsAuth and db-first EF. Additionally, all of the practices that are now standard in .Net, such as dependency injection, further improved the code.

I'm very happy with this code conversion, and my client is happy that their software will continue to be an excellent and long-lasting resource for their company.