In ASP.NET Core 2.1, a new concept or feature has been introduced by Microsoft, that is called Razor Pages. Razor pages are just like the MVC Razor engine related pages and also, quite similar to the web application pages. Today, in this article, we will discuss the concept and benefits of using Razor pages and also discuss how we can implement the Razor pages in a web application project.
WHAT ARE RAZOR PAGES?
One of the most important features in ASP.NET Core is the inclusion of Razor Pages which basically provides us similar flavor of web parts as in previous ASP.NET. So, Razor Pages can be built up with both UI and business logic to develop as a package so that we can reuse that package in any ASP.NET Core application. Obviously, in ASP.NET MVC, there is a concept of Partial Views which can also be used as a reusable UI Component. But the inclusion of business logic within a partial view in the MVC framework is quite difficult to implement. However, in Razor Pages, implementing UI and business logic is much easier and also a more natural fit with the structure of the ASP.NET Core applications.
Actually, ASP.NET Core Razor Pages is a page-oriented framework for developing or building a dynamic, data-driven web application. Razor Pages is much more lightweight and flexible as compared to the previous versions of web parts. This framework is developed on the basis of ASP.NET Core MVC framework so, it can be only activated in the ASP.NET Core application if and only if MVC is enabled in the ASP.NET Core application. The Razor Pages is one of the recommended frameworks for developing cross-platform server-side HTML in ASP.NET Core.
Razor Pages normally uses the C# programming languages syntaxes for the server-side programming.
WHY USE RAZOR PAGES?
The main reason to use Razor Pages in ASP.NET Core is that it is a much more organized process. It does not provide us the same facility just like MVC applications. In Razor Pages, we always have a Razor View and a code-behind file just like web forms in the earlier. Razor Pages always works on the Single Responsibility Principle. In simple words, Razor Pages always supports MVVM (Model-View-View-Model) framework. It always provides two-way binding and a simpler development experience with isolated concerns.
RAZOR CLASS LIBRARY PROJECT
Razor Class Library (RCL) Projects was first introduced in ASP.NET Core 2.1 for the purpose of packaging and distributions of UI Components which can be referenced and consumed within a host web application. It is basically a library project which contains Razor pages, Razor views, controllers, view components, data models etc. RCL library can be used as a reusable component. It is a very convenient way to share any web parts code with UI elements across the multiple projects. We can also package this in the NuGet Package so that other users can install that package in their own applications and use it.
CREATE A RAZOR CLASS LIBRARY FOR RAZOR UI
Step 1
Open Microsoft Visual Studio 2017 and click on File --> New --> Projects.

Step 2
Select Razor Class Library Project Template and click the OK button.

Step 3
First, rename the folder from MyFeature to Library.
Step 4
Now, add a Razor Page named _ViewStart.cshtml under the Pages folder and add the below code.
- @{
- Layout = "_Layout";
- }
Now, add a folder called Shared within the Pages folder.
Step 6
Now, add a new Razor Page within the Shared folder named _Message.cshtml and add the below code.
- <h3>This is a Partial View</h3>
- <p>This Partial view cretead as a Razor Class Library</p>
Step 7
Similarly, add another two Razor pages called _Login.cshtml and _Register.cshtml and then add the below code in the respective files.
_Login.cshtml
- <div class="container">
- <h1>Log in</h1>
- <div class="row">
- <div class="col-md-4">
- <section>
- <form id="account" method="post" novalidate="novalidate">
- <h4>Use a local account to log in.</h4>
- <hr>
- <div class="text-danger validation-summary-valid" data-valmsg-summary="true">
- <ul>
- <li style="display:none"></li>
- </ul>
- </div>
- <div class="form-group">
- <label for="Input_Email">Email</label>
- <input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address."
- data-val-required="The Email field is required." id="Input_Email" name="Input.Email" value="">
- <span class="text-danger field-validation-valid" data-valmsg-for="Input.Email" data-valmsg-replace="true"></span>
- </div>
- <div class="form-group">
- <label for="Input_Password">Password</label>
- <input class="form-control" type="password" data-val="true" data-val-required="The Password field is required." id="Input_Password" name="Input.Password">
- <span class="text-danger field-validation-valid" data-valmsg-for="Input.Password" data-valmsg-replace="true"></span>
- </div>
- <div class="form-group">
- <div class="checkbox">
- <label for="Input_RememberMe">
- <input type="checkbox" data-val="true" data-val-required="The Remember me? field is required." id="Input_RememberMe" name="Input.RememberMe" value="true">
- Remember me?
- </label>
- </div>
- </div>
- <div class="form-group">
- <button type="submit" class="btn btn-primary">Log in</button>
- </div>
- </form>
- </section>
- </div>
- </div>
- </div>
_Register.cshtml
- <div class="container">
- <h1>Register</h1>
- <div class="row">
- <div class="col-md-4">
- <form method="post" action="/Identity/Account/Register" novalidate="novalidate">
- <h4>Create a new account.</h4>
- <hr>
- <div class="text-danger validation-summary-valid" data-valmsg-summary="true">
- <ul>
- <li style="display:none"></li>
- </ul>
- </div>
- <div class="form-group">
- <label for="Input_Email">Email</label>
- <input class="form-control" type="email" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Input_Email" name="Input.Email" value="">
- <span class="text-danger field-validation-valid" data-valmsg-for="Input.Email" data-valmsg-replace="true"></span>
- </div>
- <div class="form-group">
- <label for="Input_Password">Password</label>
- <input class="form-control" type="password" data-val="true" data-val-length="The Password must be at least 6 and at max 100 characters long." data-val-length-max="100" data-val-length-min="6"
- data-val-required="The Password field is required." id="Input_Password" maxlength="100" name="Input.Password" aria-autocomplete="list">
- <span class="text-danger field-validation-valid" data-valmsg-for="Input.Password" data-valmsg-replace="true"></span>
- </div>
- <div class="form-group">
- <label for="Input_ConfirmPassword">Confirm password</label>
- <input class="form-control" type="password" data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="Input_ConfirmPassword"
- name="Input.ConfirmPassword">
- <span class="text-danger field-validation-valid" data-valmsg-for="Input.ConfirmPassword" data-valmsg-replace="true"></span>
- </div>
- <button type="submit" class="btn btn-primary">Register</button>
- </form>
- </div>
- </div>
- </div>
Step 8
Now, open the Page1.cshtml file and add the below code.
- @page
- @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
- <h2>Hello from a Razor UI class library!</h2>
- <partial name="_Message" />
Step 9
Now, if we run this page in the browser, then UI components of the _Message.cshtml file will be displayed.
Step 10
Now similarly, add two another Razor Pages called Page2.cshtml and Page3.cshtml and the below code.
Page2.cshtml
- @page
- @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
- <partial name="_Login" />
Page3.cshtml
- @page
- @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
- <partial name="_Register" />
USE RAZOR CLASS LIBRARY UI IN WEB APPLICATIONS
Step 1







Join the conversation! Your thoughts help the community grow.