Chapter 15. Reusable UI Components
So far, you have seen a variety of options for creating reusable components for your ASP.NET MVC application. However, these options allow you to create views or actions that can be reused only within a single project. In other words, they are more “shareable” components than truly “reusable ones,” since you cannot use them outside of your project without resorting to “code reuse” (aka “copy/paste”).
In this chapter, you’ll learn how to create truly reusable components that can be used as a library across different projects.
What ASP.NET MVC Offers out of the Box
Before we take a deep dive into creating cross-project reusable components, let’s take a quick look at what ASP.NET MVC offers out of the box.
Partial views allow you to create reusable content. In order to remain
truly reusable, partial views should contain little or no functional
logic, as they represent a modular unit of layout in a larger view.
Partial views are files that have the same .cshtml or
.vbhtml extension but are created under the
/Views/Shared/ folder. To render
them, you use the syntax
@Html.Partial("_partialViewName") as shown
HtmlHelper Extensions or Custom HtmlHelpers
Custom HTML helpers are extension methods applied to the
HtmlHelper class that can be used in views to output clean HTML. They follow the same rules as partial views—i.e., having no functional logic and representing a small unit of layout—but are more focused. A ...