Resolved Beginner's help needed! @ and section

Gabriel_7

New member
Joined
Sep 26, 2021
Messages
2
Programming Experience
5-10
I'm new to C# and have been looking at someone else's code and saw it had loads of @ symbol prefixes. I know how it's used for escape characters and for variables, however it's in front of almost everything, for example: @if and @section scripts. What does it mean in those scenarios....and what is a section script?

Sorry for the newb questions but I've been hunting on the internet and can't find an explanation anywhere.

Thanks!
Gabriel
 
Solution
This is not a C# General question. You must be looking at a Razor page, so this question is specific to that. Razor pages are basically HTML templates that are executed on the server to generate an HTML page that is sent to the browser. You will probably notice that there are lots of <tags> in the HTML style. Some will actually be HTML while others are tag helpers. The two basically look the same but will be coloured differently in the code editor. HTML tags will be interpreted literally while tag helpers will be translated into HTML tags. What you're talking about is how you insert C# code into a Razor page. The @ symbol indicates that what follows is C# code rather than HTML or tag helpers. For instance, you might add an...

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
This is not a C# General question. You must be looking at a Razor page, so this question is specific to that. Razor pages are basically HTML templates that are executed on the server to generate an HTML page that is sent to the browser. You will probably notice that there are lots of <tags> in the HTML style. Some will actually be HTML while others are tag helpers. The two basically look the same but will be coloured differently in the code editor. HTML tags will be interpreted literally while tag helpers will be translated into HTML tags. What you're talking about is how you insert C# code into a Razor page. The @ symbol indicates that what follows is C# code rather than HTML or tag helpers. For instance, you might add an HTML <ul> tag for an unordered list, then use a C# foreach loop to add a <li> tag for each element in an array or item in a collection:
HTML:
<ul>
    @foreach(var item in Model.Items)
    {
        <li>@item.ToString()</li>
    }
</ul>
In that case, the first @ indicates that the foreach is C# code and the second one indicates that the value in the list item is generated using C# code.
 
Solution

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
As for the sections, they are defined in the parent layout. Like Web Forms had master pages, which were a place that you could create a common template for multiple child pages, Razor has layouts. When you create an MVC or Razor Pages project, a _Layout.cshtml file will be created under Views\Shared. That is the master layout for your site, so you can import common resources there like scripts and style sheets, plus add common HTML like headers and footers. You can also add sections there and then you can populate those sections in each individual page. This gives you a way to define a common layout with holes in it in various places and then fill those holes as you see fit in each individual page. Without sections, the contents of each individual page could only be inserted into the master layout in one place.
 

Gabriel_7

New member
Joined
Sep 26, 2021
Messages
2
Programming Experience
5-10
That is really helpful and explains why I struggled to understand the code, I was so confused!

I really appreciate your detailed explanations, thanks so much!
 
Top Bottom