Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Common Grammar examples in Razor

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "Razor commonly used grammar case analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "Razor common grammar example analysis" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

@ using

The @ using directive is used to add a C # using directive to the generated view:

@ using System.IO@ {var dir = Directory.GetCurrentDirectory ();}

@ dir

@ page

The @ page directive has different effects, depending on the type of file it is in. Directive:

Indicates that the file is Razor Page** in a .cshtml file. For more information, see the introduction to the Razor page in Custom routing and ASP.NET Core.

Specifies that the Razor component should process the request directly. For more information, see ASP.NET Core Blazor routing.

@ namespace

Sets the namespace of the generated Razor page, MVC view, or class of the Razor component.

@ layout

The @ layout directive specifies the layout of the Razor component. Layout components are used to avoid code duplication and inconsistencies.

@ inject

The @ inject directive allows Razor pages to inject services from the service container into the view.

@ implements

The @ implements directive implements the interface for the generated class.

The following example implements System.IDisposable so that the Dispose method can be called:

Example@functions {private bool _ isDisposed;... Public void Dispose () = > _ isDisposed = true;} @ code

The @ code block allows the Razor component to add C# members (fields, attributes, and methods) to the component.

@ code {/ / C # members (fields, properties, and methods)} @ attribute

The @ attribute directive adds the given attribute to the class of the generated page or view. The following example adds a [Authorize] attribute:

@ attribute [Authorize] comment @ {/ * C# comment * / Another C# comment} escape

If you need to escape for @, you can use @ @, because @ is the syntax of razor.

Render HTML@ ("Hello World") Hello Worldtry, catch, finally

Similar to the C# syntax, you can declare it with the @ prefix.

{throw new InvalidOperationException ("You did something invalid.");} catch (Exception ex) {

The exception message: @ ex.Message

} finally {

The finally statement.

} Loop statements for, foreach, while and dowhile

@ for

@ for (var I = 0; I

< people.Length; i++){ var person = people[i]; Name: @person.Name Age: @person.Age } @foreach @foreach (var person in people){ Name: @person.Name Age: @person.Age } @while @{ var i = 0; }@while (i < people.Length){ var person = people[i]; Name: @person.Name Age: @person.Age i++;} @do while @{ var i = 0; }@do{ var person = people[i]; Name: @person.Name Age: @person.Age i++;} while (i < people.Length);条件语句 if、else if、else 和 switch@if (value % 2 == 0){ The value was even. }@if (value % 2 == 0){ The value was even. }else if (value >

= 1337) {

The value is large.

} else {

The value is odd and small.

} @ switch (value) {case 1:

The value is 1!

Break; case 1337:

Your number is 1337!

Break; default:

Your number wasn't 1 or 1337.

Break;} read here, this "Razor commonly used grammar example analysis" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report