Blazor vs. Razor | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)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

Be the first to get updates

Subscribe RSS feed