IC Improve Code Recipes

Chapter 16

From Web Forms to ASP.NET Core

ASP.NET Web Forms paid a lot of mortgages, mine included. From 2002 until well into the 2010s it was how most line-of-business web applications on the Microsoft stack were built, and plenty of those applications are still running. The archive behind this book holds thousands of .aspx pages, and a hand-written BasePage class, the one every page inherits from, appears in 9 engagements. Session and state classes show up in 27 engagements, and roughly 39,595 C# files (9.1%) read or write Session["..."] directly, in 25 engagements.

Web Forms will not come to ASP.NET Core. Microsoft has said so plainly, and the 4.8 line is the last version of .NET Framework. The framework is supported as part of Windows, so nothing stops working tomorrow, but every year it gets harder to hire for, harder to host and further away from the libraries everyone else uses.

The good news is that most of what a Web Forms application does has a clear modern equivalent. The BasePage becomes filters and middleware. Session["..."] becomes a typed wrapper. The paged GridView becomes a small paging model and a partial view. The Web.sitemap becomes a navigation section in configuration. And the big-bang rewrite that every team fears can be replaced with an incremental migration, one route at a time.

This chapter shows each of those moves. The listings build up a single ASP.NET Core MVC application; the last recipe puts the pieces together in Program.cs alongside the legacy app.

Recipes

  1. 16.1Replacing the BasePage with FiltersEvery page in the application inherits from a BasePage class. It checks that the user is logged in, loads the user's permissions, decides whether…
  2. 16.2From Global.asax and HttpModules to MiddlewareRequest-wide behavior lives in Global.asax and a few IHttpModule classes: a maintenance-mode switch, security headers, URL rewrites, error mail. The…
  3. 16.3A Typed Session WrapperThe application stores everything in session: the user, the selected customer, the search filters, a half-finished order, sometimes a whole DataSet.…
  4. 16.4A Reusable Paging Model for GridsGrids page through data in almost every screen. The Web Forms version loads every row into a DataSet, binds it to a GridView with AllowPaging="true",…
  5. 16.5Navigation Menus from ConfigurationThe menu is either a Web.sitemap file with role trimming that nobody fully understands, or a master page code-behind full of if (User.IsInRole(...))…
  6. 16.6Migrating Incrementally with YARP and System.Web AdaptersThe Web Forms application has hundreds of pages and a decade of business rules. A full rewrite would take a year or more, with nothing delivered…

Chapter checklist

  • List what your BasePage and Global.asax actually do, and map each job to a fallback policy, an authorization filter, an action filter or middleware.
  • Turn on a fallback authorization policy so new endpoints are protected by default, and mark the public ones with [AllowAnonymous].
  • Put every session key behind a typed IUserSession; store IDs and small records only, and keep identity in the authentication cookie.
  • Remember that ASP.NET Core session is not locked per request, and move anything that must stay consistent into the database.
  • Page in the database with OFFSET/FETCH, clamp page and size input, and reuse one PagedResult<T> and one pager partial.
  • Move the menu into configuration and tie each item to the same authorization policy that protects the page.
  • Put an ASP.NET Core app with a YARP forwarder in front of the Web Forms app, and migrate one route at a time, starting this sprint with one read-only screen.