Adding Razor cshtml view runtime re-compilation to a ASP.NET Core 5.0 app after creating it

I recently came across an interesting issue where after starting a new ASP.NET Core 5.0 .NET 5 project using the “ASP.NET Core Web App (Model-View-Controller)” template did not include the ability to update .cshtml Razor files without recompiling and restarting the whole app. There is a checkbox to “Enable Razor Runtime Compilation” during project setup but it’s easy to miss and tricky to add afterwards if you don’t know what you are looking for.

First, you need to head to the Nuget Package explorer by right clicking on the Dependencies node of your project tree selecting “Manage NuGet packages…”. Search for and install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation. Install it and it’s dependencies into your project.

Then head to Startup.cs. Find the line within ConfigureServices() that says “services.AddControllersWithViews()” and add “.AddRazorRuntimeCompilation()”. You won’t be able to add this line without first installing the previous NuGet package.

Now run your app. You will see that changing the contents of Razor .cshtml files and refreshing the page will cause the view to be updated with your new content, without having to stop and recompile.

YouTube demonstration video

Razor Runtime Compilation - Reload Razor view in .NET Core 5.0 without recompiling
Watch this video on YouTube.

ASP.NET MVC Basics Part 2: ViewModel to Model Mapping and Editing

In Part 1, I walked through creating a simple form with a backing ViewModel and Validation. In Part 2, I’ll walk through creating a backing Model and Edit functionality.

To start off from here, load up the code from part1:

The final code for Part 2 will be uploaded here:

Model Mapping flow


When editing a ViewModel, we need to prepopulate the view with real data from a domain model. This can be a database table mapped to an object via an ORM or data from another source. In the diagram above, we can see the GET Edit action requesting Model with an ID of 123 from the Repository holding the model data, creating a ViewModel that represents the Model and passing it onto the View to render. POSTing the data back is similar to the Create POST method in Part 1, except we load the existing Model from the repository, update it with the validated data from the ViewModel and update the model in the Repository.

Read more

ASP.NET MVC Basics Part 1: View Model binding

I’m going to walk through the basics of Form submission with ASP.NET MVC, showing some best practices. This set of tutorials will be useful for developers moving away from ASP.NET WebForms into ASP.NET MVC or even Rails developers curious about how we do things in .NET.

You can download the code for Part 1 at:

Form submission flow

If you have come from WebForms, you’ll be used to being able to pull form values out in the code behind by simply referencing variables in your code behind. These magically map to elements on the page and most of the time you are blissfully unaware how the data gets there. With MVC, we don’t have the same abstraction. Whereas you can access POSTed variables directly with FormsCollection (or params in Rails) but with the ViewModel pattern, we can simulate the binding that ASP.NET provides and access our form variables in a strongly typed manner.


Read more