Microsoft is pitching its Blazor framework, the newest addition to the ASP.NET Core platform, as a suitable upgrade for C# developers still wedded to Web Forms, the programming model dating back to the first release of the .NET Framework in 2002.
Web Forms were designed to make the transition to web applications easy for desktop developers. There is a visual designer, a toolbox with controls like buttons and listboxes, and developers can double-click a control and write C# code that runs on the server.
Despite its age, Microsoft said last year that “almost half a million web developers use ASP.NET web forms every month.”
That is in some ways curious, since it is over a decade since Microsoft released ASP.NET MVC (2009), a web framework intended to solve the issues with web forms. ASP.NET MVC – where the MVC stands for Model View Controller – has controller files which handle routing and some business logic, and a view engine which renders web pages.
Razor has endured, though ASP.NET MVC has given way to Razor Pages, similar but with a more logical design (and acknowledging that the “MVC” aspect was never pure). Although ASP.NET MVC or Razor Pages is now the mainstream choice, why have so many stuck with web forms? The answer perhaps is that it is easier to learn, and lets developers write all their code in C# or Visual Basic; if the goal is a solution to a business problem, it is often good enough. There are parallels with Microsoft’s desktop frameworks, where Windows Presentation Framework fixed many issues with the older Windows Forms (scaling, graphics acceleration, rich design) but its greater complexity meant that many developers refused to shift.
Microsoft is continuing to support web forms, but the technology is now frozen and has not been ported to ASP.NET Core. It is largely Windows-only, though cross-platform Mono has some support).
Enter Blazor, the newest web application framework to come out of Microsoft’s ASP.NET team. Blazor appeared on the scene in early 2018. Microsoft apparently believes this may be the thing that grabs some Windows Forms holdouts and has provided an ebook on how to migrate, though it also stated that “migrating a code base from ASP.NET Web Forms to Blazor is a time-consuming task.”
A Blazor application has the code in a standard .NET assembly but the user interface is standard HTML and CSS
Blazor is not very like web forms but has some things in common. One is that developers can write C# everywhere, both on the server and for the browser client. Microsoft calls this “full stack C#”.
Having a peek at Blazor WebAssembly is easy. Install the SDK – for example the hot new .NET 5.0, set for release next week – and type:
dotnet new blazorwasm -o BlazorHello
dotnet watch run
to run the sample application. It is instructive to look at a release build. There is BlazorHello.dll, a assembly like any other.
The sample app notes “Loaded 8.67 MB resources. This application was built with linking (tree shaking) disabled. Published applications will be significantly smaller.”
The Blazor runtime files are alarmingly bulky but compression and eliminating unused code brings the size down for published applications
Blazor is designed for single-page applications and is reminiscent of Silverlight – Microsoft’s browser plugin in which ran .NET code in the browser – but with an HTML/CSS user interface.
There are two other Blazor application models. Blazor Server runs on the server and supports a thin browser client communicating with WebSockets (ASP.NET SignalR). The programming model is the same, but it is a thin client approach which means faster loading and no WebAssembly required; it can even be persuaded to run in IE11.
Then there is Mobile Blazor, recently updated to Preview 5, which uses the Blazor programming model but with either HTML UI or Xamarin forms (native UI) to run as a mobile application.
Although the mobile bindings are described as experimental it looks like Microsoft is serious about the project. The new preview adds support for SkiaSharp graphics, gesture recognisers, dual-screen support, DataPicker, TimePicker, simplified Grid layout and more.
The programming model feels like a cross between Razor Pages and web forms. It is easier than Razor Pages for getting started, but uses the same Razor syntax. State is held in the browser during a session; it is a single-page application so the round-trip issues in web forms do not exist, unless the user unwisely refreshes the page.
Persisting state across sessions is via standard web technology, such as interacting with a database on the server, but with built-in support for local browser storage to enable offline use. Building as a PWA (Progressive Web Application) is a built-in option. Developers can also use Microsoft’s identity system to get instant support for registration, log-in, authentication and so on; or they can use Azure Active Directory.
A big win for productivity is that code can be shared between the client and the server and works the same way. Developers can set up a project with shared classes. This makes it easier to work with the same business objects on client and server.
Blazor in .NET 5.0 is substantially improved. The big deal is that “Blazor WebAssembly in .NET 5 is two to three times faster for most scenarios,” according to the release notes. Other changes include component-specific CSS, ability to set UI focus in code, new InputFile component for file upload, Microsoft Identity v 2.0, and more.