Simple Blazor Components In .NET CORE MVC 3.1

Introduction 

 
Hello Folks,
 
In this article, we will add a Razor component to our existing .NET Core MVC project.
 
Blazor is a framework for building interactive client-side web UI with .NET. With it, you can:
  • Create rich interactive UIs using C# instead of JavaScript.
  • Share server-side and client-side app logic written in .NET.
  • Render the UI as HTML and CSS for wide browser support, including mobile browsers.
Blazor apps are based on components. A component in Blazor is an element of UI, such as a page, dialog, or data entry form.
 
Let's start with a few simple steps.
 
Step 1
 
Simple Blazor Components Use In .NET CORE MVC 3.1
 
Step 2
 
Simple Blazor Components Use In .NET CORE MVC 3.1
 
Step 3
 
Add a Microsoft.AspNetCore.Components reference to your project if not available.
 
Simple Blazor Components Use In .NET CORE MVC 3.1
 
Step 4
 
Add a Component folder inside the view/shared folder, then add a Razor component.
 
Simple Blazor Components Use In .NET CORE MVC 3.1
 
Step 5
 
For the Razor component we have written a simple method named "GetData", which you can see below. Also, I have made 2 public properties for getting data from the view and decorating it with the attribute name as Parameter.    
  1. <div class="card-header">  
  2.   
  3.     <h3>DataComponent</h3>  
  4.     <button @onclick="Getdata" class="btn btn-dark">Click to GetData    </button>  
  5. </div>  
  6. <div class="card-body">  
  7.     <br />  
  8.     <div class="@style">@Data </div>  
  9. </div>  
  10.   
  11. @code {  
  12.     [Parameter]  
  13.     public string Data { getset; } = string.Empty;  
  14.     [Parameter]  
  15.     public string style { getset; }  
  16.     private void Getdata()  
  17.     {  
  18.         Data = "I am Working";  
  19.         style = "badge-success";  
  20.   
  21.     }  
  22.   
  23. }  
Simple Blazor Components Use In .NET CORE MVC 3.1
 
Step 6
 
The below code you need to add to your _Layout.cshtml  
 
This is for registering Blazor to your web application: 
  1. <base href="~/" />  
  2. <script src="_framework/blazor.server.js"></script>   
Simple Blazor Components Use In .NET CORE MVC 3.1
Step 7
 
Add simple _Imports.razor file to your root of the project which consists of the below code. These namespaces are required to access the component features over your components. 
  1. @using System.Net.Http  
  2. @using Microsoft.AspNetCore.Authorization  
  3. @using Microsoft.AspNetCore.Components.Authorization  
  4. @using Microsoft.AspNetCore.Components.Forms  
  5. @using Microsoft.AspNetCore.Components.Routing  
  6. @using Microsoft.AspNetCore.Components.Web  
  7. @using Microsoft.JSInterop  
  8.   
  9. @using System.IO   
Simple Blazor Components Use In .NET CORE MVC 3.1
 
Step 8
 
Add services.AddServerSideBlazor(); to your startup file method name ConfigureServices.
 
Add endpoints.MapBlazorHub(); to your Configure method.  
  1. public void ConfigureServices(IServiceCollection services)  
  2.     {   
  3.         services.AddServerSideBlazor();  
  4.         services.AddControllersWithViews();  
  5.     }  
  6.   
  7.     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  8.     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  9.     {  
  10.         if (env.IsDevelopment())  
  11.         {  
  12.             app.UseDeveloperExceptionPage();  
  13.         }  
  14.         else  
  15.         {  
  16.             app.UseExceptionHandler("/Home/Error");  
  17.             // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.  
  18.             app.UseHsts();  
  19.         }  
  20.         app.UseHttpsRedirection();  
  21.         app.UseStaticFiles();  
  22.   
  23.         app.UseRouting();  
  24.   
  25.         app.UseAuthorization();  
  26.   
  27.         app.UseEndpoints(endpoints =>  
  28.         {  
  29.             endpoints.MapControllerRoute(  
  30.                 name: "default",  
  31.                 pattern: "{controller=Home}/{action=Index}/{id?}");   
  32.             endpoints.MapBlazorHub();  
  33.         });  
  34.   
  35.     }  
Simple Blazor Components Use In .NET CORE MVC 3.1
Step 9
 
The last step is to render components on your View page.
 
You can see that I am passing 2 parameters as requested in our DataViewComponent.razor file 
  1. @{  
  2.     ViewData["Title"] = "Home Page";  
  3. }  
  4. <div class="text-center">  
  5.     <h1 class="display-4">Welcome</h1>  
  6.       
  7. </div>  
  8.   
  9. <div class="card">  
  10.      
  11.         @(await Html.RenderComponentAsync<BlazorComponentProject.Views.Shared.Components.DataViewComponent>(RenderMode.ServerPrerendered,new {  Data="I came from Index",style= "badge-danger" }))  
  12.            
  13.       
  14. </div>   
Simple Blazor Components Use In .NET CORE MVC 3.1

Summary

 
Our main focus in this article was to help you to integrate your existing Web application build in .NET Core MVC 3.1 with Blazor and use the Blazor components in it. 
 
Please refer to the project and debug the process for more understanding. I hope it helps!