---
title: "How to create reusable components or templates in Razor views?"  
description: "How to create reusable components or templates in Razor views?"  
author: "Sandra Emily"  
published: 2023-10-20  
updated: 2023-10-25  
canonical: https://www.mindstick.com/forum/160236/how-to-create-reusable-components-or-templates-in-razor-views  
category: ".net core"  
tags: ["asp.net core", ".net core", "razor view"]  
reading_time: 2 minutes  

---

# How to create reusable components or templates in Razor views?

How to create [reusable components](https://www.mindstick.com/articles/337380/best-practices-in-react-for-creating-reusable-components) or [templates](https://www.mindstick.com/articles/12874/crucial-benefits-of-using-powerpoint-templates-in-your-business-presentations) in [Razor](https://www.mindstick.com/articles/12002/asp-dot-net-mvc5-razor-with-jquery) [views](https://www.mindstick.com/forum/12838/compile-views-in-asp-dot-net-mvc)?

## Replies

### Reply by Aryan Kumar

Creating reusable [components](https://www.mindstick.com/blog/74096/understanding-the-role-of-some-integral-ac-components) or templates in Razor views in ASP.NET Core is a powerful way to maintain consistency in your application's user interface and reduce code duplication. You can achieve this through the use of Razor Partial Views, View Components, or Razor Pages.

## 1. Razor Partial Views:

Razor Partial Views are reusable components that allow you to create modular pieces of your view and include them in multiple views. Here's how to create and use them:

## Create a Partial View:

1. Create a new Razor view file with a name that starts with an underscore (e.g., **_MyPartialView.cshtml**). This naming convention signifies that it's a partial view.
2. Define the HTML and content for your partial view in this file.

## 2. View Components:

View Components are more advanced and versatile than partial views, as they can include both HTML and associated logic. Here's how to create and use them:

## Create a View Component:

1. Create a new class that derives from **ViewComponent** (e.g., **MyViewComponent.cs**).
2. Implement the **Invoke** method to perform any necessary logic and return a **View** result with your HTML.

## 2. View Components:

View Components are more advanced and versatile than partial views, as they can include both HTML and associated logic. Here's how to create and use them:

## Create a View Component:

1. Create a new class that derives from **ViewComponent** (e.g., **MyViewComponent.cs**).
2. Implement the **Invoke** method to perform any necessary logic and return a **View** result with your HTML.

The choice between partial views, view components, and Razor Pages depends on your specific requirements. Partial views are simple and best suited for UI components, while view components are more versatile and allow for associated logic. Razor Pages are suitable when you need to create full pages with associated code. By using these techniques, you can create a more modular and maintainable UI in your ASP.NET Core application.


---

Original Source: https://www.mindstick.com/forum/160236/how-to-create-reusable-components-or-templates-in-razor-views

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
