Blazor vs. Razor | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (163).NET Core  (29).NET MAUI  (83)Angular  (82)ASP.NET  (50)ASP.NET Core  (79)ASP.NET MVC  (88)Azure  (38)Blazor  (160)DocIO  (23)Essential JS 2  (89)Essential Studio  (180)File Formats  (33)Flutter  (123)JavaScript  (190)Microsoft  (114)PDF  (68)React  (65)Succinctly series  (129)Syncfusion  (638)TypeScript  (32)Uno Platform  (3)UWP  (4)Vue  (40)Webinar  (35)Windows Forms  (59)WinUI  (56)WPF  (140)Xamarin  (157)XlsIO  (26)Other CategoriesBarcode  (5)BI  (29)Bold BI  (5)Build conference  (6)Business intelligence  (54)Button  (4)C#  (121)Chart  (59)Cloud  (13)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (6)DataGrid  (56)Development  (420)Doc  (7)DockingManager  (1)eBook  (97)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (24)Extensions  (19)File Manager  (4)Gantt  (14)Gauge  (11)Git  (4)Grid  (28)HTML  (12)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (336)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (33)Performance  (10)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (8)Road Map  (11)Scheduler  (39)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (23)Solution Services  (3)Spreadsheet  (11)SQL  (8)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (96)UI  (240)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (23)Visual Studio Code  (17)Web  (398)What's new  (229)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Blazor vs. Razor

Blazor vs. Razor

Blazor and Razor are two important terms when it comes to .NET-based web application development.

This article will explore:

  • What is Blazor?
  • How is Razor related to Blazor?
  • How is Razor employed in ASP.NET Core MVC, ASP.NET Core Razor Pages, and Blazor?

What is Blazor?

Blazor is a component-based, single-page app framework that works with all modern web browsers, including mobile browsers. The main difference between other popular web frameworks and Blazor is that the latter uses C# instead of JavaScript to build web apps, allowing developers to create both the client-side and server-side of an app using the same language, C#.

Like React, Blazor has a reusable-component-based structure to build components that we can use over multiple pages. In addition, Blazor comprises two different hosting models, Blazor WebAssembly and Blazor Server.

Blazor WebAssembly

When Blazor WebAssembly (WASM) is used, all the processing is done on the client-side by downloading the dependencies in the browser. Because of this, there is no need for a constant connection with the server.

Blazor Server

As the name suggests, the app is hosted on a server from within the standard ASP.NET Core app. Unlike in Blazor WebAssembly, this needs a constant connection between the Blazor Server app and the browser through SignalR.

What is Razor?

Razor is markup syntax based on C# and HTML. The Razor syntax is used to bind data, process logic, implement conditional statements, and more.

Refer to the following screenshot of a code example.
Razor code example screenshot

What are Razor Pages?

Razor Pages is a programming model that make project development more productive than when using views and controllers. ASP.NET Core projects are classified into ASP.NET Core MVC and ASP.NET Core Razor Pages.

ASP.NET Core MVC: Model-View-Controller (MVC) pattern. Represented as Controller/ControllerName.cs and Views/ViewName.cshtml file.

ASP.NET Core Razor Pages: Page-focused approach away from view and controller. Represented as RazorPageName.cshtml and RazorPageName.cshtml.cs.

Razor provides the syntax for both ASP.NET Core MVC and ASP.NET Core Razor Pages.

Relationship between Razor and Blazor

As the name of Blazor comes from combining the two terms “Browser” and “Razor”, we can see that there’s a relationship between the Blazor and Razor.

Blazor components are known as Razor Components. Blazor components are represented with the .razor file extension. In Blazor, the Razor syntax is used for client-side UI logic:

  • Using Razor components with Blazor allows developers to break down individual features into tiny, easy-to-build components and then assemble these components to build a much larger app efficiently.

In the following segment, I explain with examples how to use the Razor syntax in ASP.NET Core Razor Pages, ASP.NET Core MVC, and Blazor.

ASP.NET Core Razor Pages with Razor syntax

Create a Razor Pages project referring to the Tutorial: Get started with Razor Pages in ASP.NET Core. Then, follow these steps:

  1. Create a Models folder in the root location.
  2. Create the model class ProfileModel.cs with the following code.
    Public class ProfileModel
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; } = string.Empty;
        public DateOnly DOB { get; set; }
        public string Mail { get; set; } = string.Empty;
    }
  3. In the Index.cshtml.cs file, populate a list with employee data.
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using WebApplication2.Models;
    
    namespace WebApplication2.Pages
    {
        public class IndexModel : PageModel
        {
            private readonly ILogger<IndexModel> _logger;
    
            public IndexModel(ILogger<IndexModel> logger)
            {
                _logger = logger;
            }
            public List<ProfileModel> EmpData { get; set; } = new List<ProfileModel>();
            public void OnGet()
            {
                EmpData.Add(new ProfileModel
                {
                    EmpId = 1201,
                    DOB = new DateOnly(1980, 1, 14),
                    EmpName = "Fleet Janet",
                    Mail = "fleet32@sample.com"
                });
    
                EmpData.Add(new ProfileModel
                {
                    EmpId = 1202,
                    DOB = new DateOnly(1982, 4, 12),
                    EmpName = "Dodsworth Margaret",
                    Mail = "dodsworth107@jourrapide.com"
                });
    
                EmpData.Add(new ProfileModel
                {
                    EmpId = 1203,
                    DOB = new DateOnly(1981, 7, 24),
                    EmpName = "Nancy Leverling",
                    Mail = "nancy16@mail.com"
                });
            }
        }
    }
  4. In the .cshtml file, access the model class data and process it.
    @page
    @model IndexModel
    @using WebApplication2.Models
    @{
        ViewData["Title"] = "Home page";
    }
    
    @if (Model != null && Model.EmpData != null)
    {
        <table>
            <thead><tr><td>ID</td><td>Name</td><td>DOB</td><td>Mail</td></tr></thead>
            <tbody>
                @foreach (ProfileModel person in Model.EmpData)
                {
                    <tr>
                        <td>@person.EmpId</td>
                        <td>@person.EmpName</td>
                        <td>@person.DOB</td>
                        <td>@person.Mail</td>
                    </tr>
                }
            </tbody>
        </table>
    }

That’s it. We have created an ASP.NET Core Razor Pages project with Razor syntax.

ASP.NET Core MVC with Razor syntax

Create an ASP.NET Core MVC project referring to the Get started with ASP.NET Core MVC documentation. Then, follow these steps:

  1. In the Model folder, create a ProfileModel class with the following code.
    public class ProfileModel
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; } = string.Empty;
        public DateOnly DOB { get; set; }
        public string Mail { get; set; } = string.Empty;
    }
  2. Create the class file EmpData.cs and populate employee data.
    public static class EmpData
    {
       public static List<ProfileModel> GetEmpData()
       {
           List<ProfileModel> List = new List<ProfileModel>();
           List.Add(new ProfileModel
           {
              EmpId = 1201,
              DOB = new DateOnly(1980, 1, 14),
              EmpName = "Fleet Janet",
              Mail = "fleet32@sample.com"
           });
    
           List.Add(new ProfileModel
           {
              EmpId = 1202,
              DOB = new DateOnly(1982, 4, 12),
              EmpName = "Dodsworth Margaret",
              Mail = "dodsworth107@jourrapide.com"
           });
    
           List.Add(new ProfileModel
           {
               EmpId = 1203,
               DOB = new DateOnly(1981, 7, 24),
               EmpName = "Nancy Leverling",
               Mail = "nancy16@mail.com"
           });
    
           return List;
        }
    }
  3. Access data in the HomeController file and pass the data to the View template through model binding.
    public IActionResult Index()
    {
        return View(EmpData.GetEmpData());
    }
  4. In the Index.cshtml file, receive the model class, and process it.
    @model List<ProfileModel>
    @{
        ViewData["Title"] = "Home Page";
    }
    
    @if (Model != null)
    {
        <table>
            <thead><tr><td>ID</td><td>Name</td><td>DOB</td><td>Mail</td></tr></thead>
            <tbody>
                @foreach (ProfileModel person in Model)
                {
                    <tr>
                        <td>@person.EmpId</td>
                        <td>@person.EmpName</td>
                        <td>@person.DOB</td>
                        <td>@person.Mail</td>
                    </tr>
                }
            </tbody>
        </table>
    }

We have successfully created an ASP.NET Core MVC project with Razor syntax.

Razor components in Blazor

Create a Blazor WebAssembly application by referring to this documentation. In the Blazor WebAssembly app, add the following code in the Razor component.

@page "/"

@if (EmpData.GetEmpData() != null)
{
    <table>
        <thead><tr><td>ID</td><td>Name</td><td>DOB</td><td>Mail</td></tr></thead>
        <tbody>
            @foreach (ProfileModel person in EmpData.GetEmpData())
            {
                <tr>
                    <td>@person.EmpId</td>
                    <td>@person.EmpName</td>
                    <td>@person.DOB</td>
                    <td>@person.Mail</td>
                </tr>
            }
        </tbody>
    </table>
}

@code
{
    
    public class ProfileModel
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; } = string.Empty;
        public DateOnly DOB { get; set; }
        public string Mail { get; set; } = string.Empty;
    }

    public static class EmpData
    {
        public static List<ProfileModel> GetEmpData()
        {
            List<ProfileModel> List = new List<ProfileModel>();
            List.Add(new ProfileModel
            {
                 EmpId = 1201,
                 DOB = new DateOnly(1980, 1, 14),
                 EmpName = "Fleet Janet",
                 Mail = "fleet32@sample.com"
             });

            List.Add(new ProfileModel
            {
                 EmpId = 1202,
                 DOB = new DateOnly(1982, 4, 12),
                 EmpName = "Dodsworth Margaret",
                 Mail = "dodsworth107@jourrapide.com"
            });

            List.Add(new ProfileModel
            {
                 EmpId = 1203,
                 DOB = new DateOnly(1981, 7, 24),
                 EmpName = "Nancy Leverling",
                 Mail = "nancy16@mail.com"
            });

         return List;
      }
   }
}

We have created the Blazor application with Razor components.

Conclusion

This article discussed the relationship between Blazor and Razor.

Thank you for reading!

Syncfusion’s Blazor component suite offers over 70 UI components that work with both server-side and client-side (WebAssembly) hosting models seamlessly. Use them to build marvelous apps!

If you have any questions or comments, you can contact us through our support forumssupport portal, or feedback portal. We are always happy to assist you!

Related Blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top