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:
- Return to Visual Studio.
- Right-click the “Pages” folder and select “Add, Class” from the menu.
- Name the new class
Counter.razor.cs
. - Update the
Counter
class to be a partial class by using thepartial
keyword:
Get hands-on with 1400+ tech skills courses.