Using Partial Classes to Separate Markup from Code

Learn how to use partial classes with Razor components.

We'll cover the following

Many developers prefer to separate their markup from their C# fields, properties, and methods. Since Razor components are regular C# classes, they support partial classes. The partial keyword is used to create a partial class. We will use a partial class to move the code block from the RAZOR file to a CS file. We do this as follows:

  1. Return to Visual Studio.
  2. Right-click the “Pages” folder and select “Add, Class” from the menu.
  3. Name the new class Counter.razor.cs.
  4. Update the Counter class to be a partial class by using the partial keyword:

Get hands-on with 1400+ tech skills courses.