Understand the similarities and differences between Blazor and ASP.NET Core MVC in one article

Both Blazor (rookie) and ASP.NET Core MVC (veteran) are web application frameworks developed by Microsoft . The main differences between them are:

1. Programming language: Blazor uses C# and Razor syntax , while ASP.NET Core MVC uses C# or F#.

2. Client interaction: Blazor can run C# code on the client side using WebAssembly technology , while ASP.NET Core MVC runs C# code on the server side, and then sends HTML and JavaScript to the client .

3. Componentization: Blazor is a componentization framework that allows developers to create reusable components, while ASP.NET Core MVC is a framework based on controllers and views.

4. Performance: Blazor has better performance than ASP.NET Core MVC because it can run C# code on the client side, reducing the communication between the server and the client.

In general, Blazor is more suitable for web applications that require high performance, reusable components, and client-side interaction, while ASP.NET Core MVC is more suitable for traditional controller and view-based web applications.

Supplement - Razor basic syntax:

C# Razor is a view engine for ASP.NET Web applications that allows you to embed C# code in HTML . Here are some common C# Razor syntaxes:

1. Output variable or expression:

```
<p>Welcome to @Model.Title</p>
```

2. Use conditional statements:

```
@if (Model.IsLoggedIn) {     <p>Welcome back, @Model.UserName</p> } else {     <p>Please log in first</p> } ```




3. Use a loop statement:

```
<ul>
@foreach (var item in Model.Items) {
    <li>@item.Name</li>
}
</ul>
```

4. Define local variables:

```
@{     var count = Model.Items.Count; } <p>Total @count items</p> ```



5. Use the HTML helper method:

```
@Html.ActionLink("View Details", "Details", new { id = Model.Id })
```

6. Using Razor blocks:

```
@{     var message = "Welcome to my website!"; } @section header {     <h1>@message</h1> } ```





7. Using Razor templates:

```
@helper FormatName(string firstName, string lastName) {
    <p>@lastName, @firstName</p>
}
@FormatName("张", "三")
```

Guess you like

Origin blog.csdn.net/BlueCapt/article/details/131165450