MVC vs Blazor

Trying to decide which .NET framework you should use for your next project? Relying on my personal knowledge and experiences, I aim to cover the key differences between the two to help you decide.

clock
me
Justin Furst

Hello, my name is Justin Furst and I have been developing full stack .NET applications for six years. During that time, I have built applications using WPF, Windows Forms, MVC and Blazor. Today's article will be focusing on the latter two. I initially learned web development using the MVC framework and was quickly blown away by the power it held. However, I decided to transition to Blazor last year and I can't believe how intuitive it is and best of all, no JavaScript!


If you are not familiar, ASP .NET MVC was released in 2007 and introduced the razor file syntax in 2011. These files allow you to use C#, HTML and CSS all within the same file. As you can imagine, this was groundbreaking, allowing you to embed custom HTML elements within if-else statements. This further reduced the barriers to entry for backend developers into the world of web development. MVC is split into three main components: models, views and controllers. Controllers handle pulling data and calculations, views are basically HTML templates that display variables and models are populated in a controller and passed to the views. On top of this, there is a view bag that can be passed from controller to view pages and is basically a black bag that you can store anything; kind of dangerous.

MVC Framework

Blazor was released 11 years later in 2018 and similarly revolutionized web development. Blazor is a single-paged, event based web application framework. It also uses razor files which gets a little confusing because these are different from the MVC razor files. Each Blazor razor file has a code section where you can add properties and methods to it as if it were a class. These are called components and are at the core of Blazor. Blurring the lines between C# and HTML, components are used in the same way as html tags, within angled brackets and with a closing tag. Components can be embedded within components and they can pass variables and events both up and down.


As you can see, both are very powerful web development tools. Now let's take a look at their differences. One important difference is that Blazor has the option of two different project types, server side and WebAssembly. In server side, all of the heavy lifting is done on the server while web assembly actually has the user download the app from an internet browser and it runs locally on their machine, even when the machine is offline. In MVC, most of the work is done on the server with no option to change that. Another fundamental difference is that Blazor uses a web assembly based software called SignalR to keep a constant connection between the client and the server. Two great side effects of this is that the session never times out and you can have real time validation! In MVC, if you have a form where data is entered, the data can only be validated upon submission of the form. Conversely, in Blazor, as soon as the user goes to a different input box or clicks off of the input, it can be validated and an error message can be displayed without the user having to press submit.


So there you have it, the main fundamental differences between .NET MVC and Blazor. After developing multiple applications in both frameworks, I can confidently say that Blazor is the better of the two. This might seem obvious since it is the newer framework but newer does not always mean better, just look at Caddyshack 2. Blazor presents web development in a way that is old hat for many developers so once the syntax is learned, development time becomes very fast. While MVC isn't that difficult, it is less intuitive and it has features that promote lazy development. A project can very quickly convert from a proper MVC layout into one where you throw everything into the view bag and send it to the view. Which works, but leads room for security holes, errors and completely leaves out the M in MVC. It also seems that Microsoft is starting to put more of their eggs into the Blazor basket and it will be their new flagship development framework going forward.