The Razor  View Engine1
Question:What goes ina web page?               ›  asdf
But HTML isstatic. Noloops orconditionals.How do we         ›  asdfput server-sidecode in a webpage?
4	We need an interpreter on theserver and a language tocommunicate›  The interpreter is called the view engine›  Every i...
5	Our choices with ASP.NET MVC›  ASPX›  Razor›  Other   3rd Party view engines  ›  nHaml  ›  Spark  ›  NDjango  ›  ...
6	ASPX was the original viewengine in ASP.NET MVC›  Looks   like this<%!  int x = 123; !  string y = "because."; !%>!
7	Razor is the newer alternative›  Introduced   with ASP.NET MVC 3›  Honors C# and VB.Net›  Looks like this:@{ !     in...
8	Razor has some advantages›  Less   typing  ›  More  abstract code  ›  Fewer mistakes  ›  Faster to code›  Supports ...
9	Razor has an interesting syntax›  Lets   look at it over the remaining pages
10	Code block@{ !   int x = 123; !   string y = "Hello";!}!
11	Expressions›  HTMLEncoded<span>@model.Message</span>!›  Unencoded<span>!@Html.Raw(model.Message)!</span>!
12	Combining text and markup<ul>!@foreach(var item in items) {!   <li>@item.Prop</li> !}!</ul>!
13	Code and plain text@if (foo) {!    <text>Hello world</text> !}!›  … or …@if (foo) {!    @:Hello, @model.FirstName!}!
14	If the bounds of the variableare unclear, you can useparentheses<span>ISBN@(isbnNumber)</span>!
15	  What if I want to use an "@"?›  Razor   understands email addresses natively but  if it isnt an email, "@@" renders ...
16	Comments in Razor@*!This is a server side !multiline comment !*@!
17	Calling a method@(MyClass.DoSomething<Product>())!
18	Mixing text and expressionsHello @title. @name.!
19	Conclusion›  To have dynamic web pages, we need    an interpreter to run and a language to    code in›  In ASP.NET MV...
20	Further study›  Phil   Haacks quick reference   ›  http://haacked.com/archive/2011/01/06/        razor-syntax-quick-r...
Upcoming SlideShare
Loading in...5
×

06 razor

485

Published on

Published in: Design, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
485
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

06 razor

  1. 1. The Razor View Engine1
  2. 2. Question:What goes ina web page? ›  asdf
  3. 3. But HTML isstatic. Noloops orconditionals.How do we ›  asdfput server-sidecode in a webpage?
  4. 4. 4 We need an interpreter on theserver and a language tocommunicate›  The interpreter is called the view engine›  Every interpreter honors its own language
  5. 5. 5 Our choices with ASP.NET MVC›  ASPX›  Razor›  Other 3rd Party view engines ›  nHaml ›  Spark ›  NDjango ›  and more
  6. 6. 6 ASPX was the original viewengine in ASP.NET MVC›  Looks like this<%! int x = 123; ! string y = "because."; !%>!
  7. 7. 7 Razor is the newer alternative›  Introduced with ASP.NET MVC 3›  Honors C# and VB.Net›  Looks like this:@{ ! int x = 123; ! string y = "because.";!}!
  8. 8. 8 Razor has some advantages›  Less typing ›  More abstract code ›  Fewer mistakes ›  Faster to code›  Supports Intellisence›  Unit testable›  Supports layouts ›  Like master pages, but more concise
  9. 9. 9 Razor has an interesting syntax›  Lets look at it over the remaining pages
  10. 10. 10 Code block@{ ! int x = 123; ! string y = "Hello";!}!
  11. 11. 11 Expressions›  HTMLEncoded<span>@model.Message</span>!›  Unencoded<span>!@Html.Raw(model.Message)!</span>!
  12. 12. 12 Combining text and markup<ul>!@foreach(var item in items) {! <li>@item.Prop</li> !}!</ul>!
  13. 13. 13 Code and plain text@if (foo) {! <text>Hello world</text> !}!›  … or …@if (foo) {! @:Hello, @model.FirstName!}!
  14. 14. 14 If the bounds of the variableare unclear, you can useparentheses<span>ISBN@(isbnNumber)</span>!
  15. 15. 15 What if I want to use an "@"?›  Razor understands email addresses natively but if it isnt an email, "@@" renders a single @ in the response<div>!You can contact Agile Gadgets LLC atinfo@AgileGadgets.com or @@AgileGadgetson Twitter.!</div>!
  16. 16. 16 Comments in Razor@*!This is a server side !multiline comment !*@!
  17. 17. 17 Calling a method@(MyClass.DoSomething<Product>())!
  18. 18. 18 Mixing text and expressionsHello @title. @name.!
  19. 19. 19 Conclusion›  To have dynamic web pages, we need an interpreter to run and a language to code in›  In ASP.NET MVC, the language is C# and the interpreter is Razor›  Razor uses the @ sign to signal when the code begins›  We can reference variables, run methods, and handle flow control just like in regular C#
  20. 20. 20 Further study›  Phil Haacks quick reference ›  http://haacked.com/archive/2011/01/06/ razor-syntax-quick-reference.aspx›  A great Razor guide ›  http://www.asp.net/web-pages/tutorials/ basics/2-introduction-to-asp-net-web- programming-using-the-razor-syntax
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×