ASP.NET MVC 3 Razor
Version: 1.1.0
ASP.NET MVC 3 ได้แนะนำ view engine ใหม่ชื่อ Razor, เป็นสิ่งทีมีความเข้าใจและทำให้ง่ายขึ้นจาก syntax ปัจจุบันที่ใช้อยู่คือ ASP.NET web page. ใน Lab นี้ คุณจะได้เรียนวิธีการสร้าง Razor view ภายใน MVC solution. ในการเริ่มต้นของ Lab คุณจะมีความคุ้นเคยกับ Razor syntax และความสามารถของมัน, ในแบบฝึกหัดที่ 2 คุณจะเพิ่ม Razor view ใน MVC Music Store solution ที่สร้างมาให้มีการทำงานเหมือนกับคุณทำงานกับ ASP.NET view, แต่เข้าใจง่ายกว่าและโค้ดที่สั้นกว่า. และที่ Lab สุดท้าย ในแบบฝึกหัดที่ 3, คุณจะเรียนรู้วิธีการสร้างและการเรียกใช้วาน Third party ของ Razor Helpers.
คำอธิบายแบบคร่าวๆ
บันทึก:
ใน Lab นี้ คิดว่าคุณมีความรู้พื้นฐานเดียวกับ ASP.NET MVC. ถ้าคุณไม่เคยใช้ ASP.NET MVC มาก่อน, เราแนะนำให้คุณไปดู ASP.NET MVC Fundamentals Hand-on Lab.
ASP.NET MVC 3 เพิ่ม view engine ใช่ชื่อ Razor, ซึ่งเป็นสิ่งที่ง่ายต่อการเข้าใจจาก syntax ที่ใช้อยู่ในปัจจุบันของ ASP.NET web pages. ใน Lab นี้, คุณจะได้เรียนรู้วิธีสร้าง Razor views ภายใน MVC solution.
ในตอนเริ่มต้นของ Lab นี้ คุณจะ
ในตอนเริ่มต้นของ Lab นี้ คุณจะ
In the beginning of this Lab you will first get familiar with Razor syntax and its features: general rules, code blocks, inline HTML and layout pages. Once you have learned the basics, in Exercise 2 you will add Razor views in the MVC Music Store solution to provide the same functionality you have in ASP.NET views, but with a clearer and reduced code.
At the end of this lab, in Exercise 3, you will learn how to create and consume Razor Helpers to use images, grids and custom functionality.
About Razor View Engine
Many ASP.NET Web pages have C# or VB code blocks in the same place as HTML markup. In some occasions this combination is uncomfortable for writing and delimiting code. To deal with that problem, Razor was designed as an easy to learn, compact and expressive view engine that enables a fluid coding workflow.
Razor is not a new programming language itself, but uses C# or Visual Basic syntax for having code inside a page without ASP.NET delimiter: <%= %>. Razor file extension is ‘cshtml’ for C# language, and ‘vbhtml’ for Visual Basic. In this lab, we will use C# Razor with cshtml syntax.
Razor Syntax – The fundamentals
Before getting introduced to Razor you should first know some simple rules that will help you understand how to write HTML with C# or Visual Basic in the same page:
- ‘@’ is the magic character that precedes code instructions in the following contexts:
- ‘@’ For a single code line/values:A single code line inside the markup:cshtml
<p> Current time is: @DateTime.Now </p>
- ‘@{ … }’ For code blocks with multiple lines:cshtml
@{ var name = “John”; var nameMessage = "Hello, my name is " + name + " Smith"; }
- ‘@:’ For single plain text to be rendered in the page.cshtml
@{ @:The day is: @DateTime.Now.DayOfWeek. It is a <b>great</b> day! }
- ‘@’ For a single code line/values:
- HTML markup lines can be included at any part of the code:It is no need to open or close code blocks to write HTML inside a page. If you want to add a code instruction inside HTML, you will need to use ‘@’ before the code:cshtml
@if(IsPost) { <p>Hello, the time is @DateTime.Now and this page is a postback!</p> } else { <p>Hello, today is: </p> @DateTime.Now }
- Razor uses code syntax to infer indent:Razor Parser infers code ending by reading the opening and the closing characters or HTML elements. In consequence, the use of openings “{“ and closings “}” is mandatory, even for single line instructions:cshtml
// This won’t work in Razor. Content has to be wrapped between { } if( i < 1 ) int myVar=0;
Conditionals and loops with inline HTML
Here you will find examples of conditionals with inline html.
- If statement:cshtml
<p>Single line If</p> @if(result != ""){ <p>Result: @result</p> }
cshtml<p>Code block If</p> @{ var showToday = false; if(showToday){ @DateTime.Today; } else{ <text>Sorry!</text> } }
- Foreach statement:cshtml
<p> Single Line Foreach </p> <ul> @foreach (var myItem in Request.ServerVariables){ <li>@myItem</li> } </ul>
cshtml<p> Code block Foreach </p> @{ <h3>Team Members</h3> string[] teamMembers = {"Matt", "Joanne", "Robert", "Nancy"}; foreach (var person in teamMembers) { <p>@person</p> } }
- While statement:cshtml
@{ var countNum = 0; while (countNum < 50) { countNum += 1; <p>Line #@countNum: </p> } }
Comments
Comments in Razor are delimited by @**@. If you are inside a C# code block, you can also use // and /* */ comment delimiters.
cshtml
@*
A Razor Comment
*@
@{
//A C# comment
/* A Multi
line C# comment
*/
}
In this Hands-on Lab, you will learn how to:
- Create Razor Layout page templates
- Create MVC Views using Razor syntax
- Create and consume Razor Helpers
System Requirements
You must have the following items to complete this lab:
- ASP.NET and ASP.NET MVC 3
- Visual Studio 2010 Express
- SQL Server Database (Express edition or above)Note:You can install the previous system requirements by using the Web Platform Installer 3.0:http://go.microsoft.com/fwlink/?LinkID=194638.
Exercises
This Hands-On Lab is comprised by the following exercises:
- Exercise 1: Creating a Home page view using Razor syntax
- Exercise 2: Using Models in Razor views
- Exercise 3: Creating and Consuming Razor Helpers from a View
Estimated time to complete this lab: 30 minutes.
ข้อมูลจาก: http://msdn.microsoft.com/en-us/gg618477
No comments:
Post a Comment