Terminologies in Mvc:part 2 (Razor Engine Syntax vs Web Form)

Source: Internet
Author: User

by Abhishek Jaiswal:) onMar,In this article we learn about Razor Engine Syntax vs Web Form in MVC ASP.

Quick View

This article are all about the Razor Engine Syntax and traditional Web form explanations. I ' ll try to compare and differentiate between both using few parameters and some coding convention examples.

The following image says it all, but I'll try to explain all this in words and some code syntaxes.

So let's see what I had for your guys in this article. Here is some parameters based on which I ' ll compare both of the.

Implicit Code Expression

Code expressions is generally evaluated and written to the response.

Code expressions in the Razor Engine is always HTML encoded and on the other hand in Web Forms The syntax automatically E Ncodes the value.

In the following table I'll show how Yu typically display a value in a view.

Let ' s has a look:

Razor <span> @model. Message</span>
Web Form <span><%: Model. Message%></span>

Explicit Code Expression

In an explicit code expression, code expressions is evaluated and written to the response. In the following table I'll show how to typically would display a value in a view.

Let ' s has a look:

Razor <span>[email protected] (SSN) </span>
Web from <span>ssn<%: SSN%></span>

Encoded Code Expression

Let ' s has a close look at the This definition:

"HTML encoded values is done by encoded Code Expression."

Razor <span> @RazorViewEngine </span>
Web Form <span><%: Razorengineview%></span>

un-encoded Code Expression

Let ' s has a close look at the This definition:

"Un-encoded Code Expression is used for rendering un-encoded values."

In the various cases we need to explicitly render some values the should not being HTML encoded. For, we need to use the Html.raw method to ensure, the value is not encoded as determined. For this unencoded code expression with the Html.raw method comes into the action.

Let ' s has a look at code expression:

Razor <span> @Html. Raw (model. Message) </span>
Web Form <span><%: Html.raw (model. Message)%></span>

Code block/segment

Before going any further let ' s has a close look at the code block:

"Unlike code expressions, code blocks/segment is simply sections of code that is executed (evaluated and output to the R Esponse). "

    • They is useful for declaring variable and that is need to use later.
    • They is set of several code expressions.
    • evaluated one by one.

Let ' s has a look at code expression:

Razor @{
int A = 5;
String C = "Csharpcorner.";
}
Web Form <%
int A = 5;
String C = "Csharpcorner.";
%>

Combining Text and Markup

This segment of article represents what an intermixing of the text and markup looks like using both a Razor Engine and Web for M.

Let ' s has a look at code expression:

Razor @foreach (var item in items) {
<span>item @item. Name </span>
}
Web Form <% foreach (var item in items) {%>
<span>item <%: Item. Name%></span>
<%}%>

Mixing Code and Plain Text

The Razor Engine always looks for the beginning of the tag to determine when to transition from code to markup but we ofte n Get some situations where we want to output the plain text immediately after a code block. Say, displaying some plain text within a conditional block.

Let ' s has a look at code expressions:

Razor @if (ShowMessage)
{
}
Web Form <% if (showmessage)
{%>
<%}%>

Server-side Comment

Let ' s has a close look at the This definition:

"Server-side Comment syntax allows you to completely disable Content/code/controls within a page."

    • Helps in preventing the browser from parsing/running/displaying the HTML content within it.
    • Server-side comments executes on the server.
    • You can does server-side comments in both ways, either with the Razor Engine or with Web Forms.
Razor @*
Its Multiline server-side comment.
@if (ShowMessage)
{
}
This comment would be is end here.
*@
Web Form <%--
Its Multiline server-side comment.
<% if (showmessage)
{%>
<%}%>
This comment would be is end here.
--%>

Escaping the Code Delimiter

Delimiter

Before going any further let ' s has a look at some important keywords:

"A delimiter is a sequence of one or more characters used to specify the boundaries between separate, independent regions In plain text or other data streams. "

An example of a delimiter is a "Comma"

    • A delimiter specifies boundaries in the data stream, declaration notation.
    • Delimiters can broken down into field-record and bracket Delimiter.
    • caused Delimiter collision.

Escaping Delimiter

Now I'll explain what a delimiter is escaping.

Delimiter collision is a problem this occurs when a programmer introduces delimiters into code without actually intending them to be interpreted as boundaries between separate regions.

The following is methods that can is used for avoiding Delimiter collision:

    • Escaping Character
    • Escaping Sequence
    • Dual quoting Delimiter
    • Padding quoting Delimiter
    • Multiple quoting Delimiter
Razor My Twitter Handle is @jabhij
(or you can write it as:)
My Twitter Handle is @ @jabhij
Web Form &lt;% Expression%&gt; As you like it

Calling a Generic Method

Generic method

Before going any further let ' s has a look at it definition:

"A Generic method is a method which is declared with type parameters."

    • A generic method can be overloaded on several type parameters.
    • Within a generic class several non-generic methods can access several class level type parameters.
    • Constraints can used to enable more specialized operations on type parameters in methods.
Razor @ (html.mymethod<atype> ())
Web Form <%: html.mymethod<atype> ()%>

Points to Remember

The following is some important points that one needs to remember.

Razor Engine View

    • Introduced with MVC 3.0
    • Not a new language. It ' s a new "Markup Syntax"
    • Supports Test driven Dev (TDD)
    • Have new advanced syntax the is compact, expressive and reduces typing
    • Easy to learn and compression to "Web Forms"
    • Prevents cross Site Scripting (XSS) attacks
    • has different file extensions.
    • Doesn ' t support design mode.

Web Form Engine

    • Introduced with MVC 1.0
    • Doesn ' t support Test driven Dev (TDD)
    • System.Web.UI.Page Namespace is used in it for testing (but still it makes the testing complex)
    • A bit faster in comparison to Razor
    • Does not prevent from cross Site Scripting (XSS) attacks

Summary

I hope this is helpful.

A few tips from my side are, if you guys really want to become familiar with MVC then:

    • Take your time
    • Do as much practise as can (implementation)
    • Try to "Learn" things, don ' t just "Study" (I mean it)
    • Try to discuss things with friends, colleagues or in forums (C # Corner)
    • If you experience any problem and then your queries is welcome.

I hope you guys enjoyed this.

Terminologies in Mvc:part 2 (Razor Engine Syntax vs Web Form)

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.