Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @{} @code{} and @function{} in Blazor?

I am learning server-side. Blazor .

I have read the code of some projects and found some that use:

@{
}

and some that use:

@code{
}

and others that use:

@function{
}

to mark the code block.

I would like to know the difference between these. Thank you.

like image 933
Melon NG Avatar asked Oct 08 '19 00:10

Melon NG


People also ask

What is the difference between Razor and Blazor?

Razor is a templating engine that combines C# with HTML to build dynamic web content. Blazor is a component-based, single-page app framework for building client-side web apps using . NET that works well with all modern browsers via WebAssembly for client-side Blazor.

What is Razor code?

Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.

Why use Razor syntax?

The Razor syntax gives you all the power of ASP.NET, but is using a simplified syntax that's easier to learn if you're a beginner, and makes you more productive if you're an expert. Razor web pages can be described as HTML pages with two kinds of content: HTML content and Razor code.

What are Razor components?

Blazor apps are built using Razor components, informally known as Blazor components. A component is a self-contained portion of user interface (UI) with processing logic to enable dynamic behavior. Components can be nested, reused, shared among projects, and used in MVC and Razor Pages apps.


1 Answers

@ for directive(s)

@code{ } [2] should be used. @function{ } [3] is still effect, in case of method, not recommended in current version.

[2] https://docs.microsoft.com/vi-vn/aspnet/core/mvc/views/razor?view=aspnetcore-3.0#code

[3] https://docs.microsoft.com/vi-vn/aspnet/core/mvc/views/razor?view=aspnetcore-3.0#functions

See document for all cases https://docs.microsoft.com/vi-vn/aspnet/core/mvc/views/razor?view=aspnetcore-3.0#functions

like image 60
Do Nhu Vy Avatar answered Oct 04 '22 13:10

Do Nhu Vy