Usage of the @ (at) sign in ASP.

Source: Internet
Author: User

From:http://www.mikesdotnetting.com/article/258/usage-of-the-at-sign-in-asp-net

Thursday, January, 1:54 PM

The number of places where you might use or encounter, the sign in ASP. Have grown over the last @ few years and its Exact purpose in all circumstances still seems to cause confusion. Here's a overview of the most common places that it crops up, and guidance on its correct usage.

Razor Syntax

Razor is launched as a new templating syntax with the introduction of the net Web Pages framework. A New view engine is added to MVC 3 so makes use of Razor. Razor enables mixing server-side code with HTML mark up to generate a HTML response that the framework sends to the brows Er. The sign had four @ uses in Razor:

    • To open a code block
    • To denote an inline expression or statement
    • To render the value of variables
    • To render single lines of content that contain plain text or unmatched HTML tags

Code blocks is sections of C # code that does not include any output to be rendered. They is usually positioned at the top of the Web Page or View and typically contain the logic for processing a Page in We b Pages, or simple view-specific instructions in MVC. Code block start @ with the sign followed by an opening curly brace, and end with a closing curly brace:

@{    "Edit";     " ~/views/shared/_editlayout.cshtml " ;}

The content within the code block is standard C # code. A common mistake is to prefix variables declared within the code block with the sign @ . This is not necessary.

Inline expressions or statements is snippets of C # code appearing within HTML. Most often, these is used to make decisions on "to" render based on conditions, or to iterate collections for display to the browser:

<ul>    @foreach (var in rows)    {        // do something      }</ul>

Nested expressions or statements do not start with a sign @ ...

<ul>    @foreach (var in rows)    {        if  (item. Equals (x))        {            // do something        }    }</ul>

... unless they is separated from the outer expression or statement by unmatched tags

<ul>    @foreach (var in rows)    {        <li>            @if (item. Equals (x))            {                // do something            }        </li>    } </ul>

@The sign was used in Razor to render the value of variables, expressions and statements to the browser:

@DateTime. Now <!--renders the browser? X:Y) <!--renders the value of x or Y to the browser–>@Html. ActionLink ("back toList
   
     "
    "
    Index
    ") <!--renders a hyperlink-
   

Variables within expressions and statements should not being prefixed with the sign @ .

If you wish to render plain text or unmatched tags while inside a statement block, you use the sign @ followed by a CO Lon to tell Razor the what follows was not C # code:

// Plain Text  {    is//  unmatched tags{    @:<ul>}  Else {    @:<ol>}

Identifiers

An identifier in C # is the name given to a namespace, class, variable, property, method, interface etc. Rules govern what makes a valid identifier. It is permitted to use a C # keyword as an identifier and if you do, you must use the sign to @ prevent compile time er Rors. You is advised against using a keyword as an identifier, but there is times when you cannot avoid doing so.

Some overloads of the HtmlHelper classes (Web Pages and MVC) accept an object to represent the HTML attributes to be rendered a s part of the the tag that the helper represents. The following example adds a style attribute to a text input and sets it value to width:100%; :

New " width:100%; "})

When you have the creating an anonymous type with a property called to represent the style HTML attributes. If you want to set the CSS class attribute via this method, you need to add a property to the anonymous type called class -which is a C # keyword. Therefore must use the sign to enable the use of the @ class

New " Full-width "})

A mistake I see repeated quite often in the ASP forums is to apply the sign to all other properties of the @ anony MOUS type, which is just not necessary. Some people even think that's sign used here's part of the @ Razor syntax rules. It ' s not. It's usage here preceded Razor by a long.

Verbatim String literals

A verbatim string literal in C # consists of the sign @ followed by a literal string in double quotes and terminated WI Th a semi-colon e.g.

var @" Hello World ";

The benefits of using a verbatim string literal include the fact that you have need to escape double quotes (by doubling t HEM); And the string can span multiple lines in code without requiring continuation characters. For these reasons, verbatim string literals is most suitable for representing paths (which may otherwise need their slash Es escaping) and regular expression patterns (which also may otherwise require backslashes to be escaped).

New Regex (@ "\w\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}\w");

They is also useful for representing large blocks of the text if they need to being included in code in a readable manner, such As SQL statements that might is used in Web Pages applications:

var @" SELECT                 p.productname,                 o.unitprice,                 o.quantity,                 (O.unitprice * o.quantity)-(O.unitprice * O. Quantity * o.discount) as TotalCost from             orderdetails o             INNER joins products p on o.productid = P.productid             WHE RE O.orderid = @0";

The use of "the sign" in the "This" context once again have nothing to do with @ Razor syntax.

Summary

If you have ever wondered when and where you should @ is using the sign in your ASP, hopefully this article H As helped to resolve your confusion.

Usage of the @ (at) sign in ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.