06 razor
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
728
On Slideshare
728
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. The Razor View Engine1
  • 2. Question:What goes ina web page? ›  asdf
  • 3. But HTML isstatic. Noloops orconditionals.How do we ›  asdfput server-sidecode in a webpage?
  • 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 Our choices with ASP.NET MVC›  ASPX›  Razor›  Other 3rd Party view engines ›  nHaml ›  Spark ›  NDjango ›  and more
  • 6. 6 ASPX was the original viewengine in ASP.NET MVC›  Looks like this<%! int x = 123; ! string y = "because."; !%>!
  • 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 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 Razor has an interesting syntax›  Lets look at it over the remaining pages
  • 10. 10 Code block@{ ! int x = 123; ! string y = "Hello";!}!
  • 11. 11 Expressions›  HTMLEncoded<span>@model.Message</span>!›  Unencoded<span>!@Html.Raw(model.Message)!</span>!
  • 12. 12 Combining text and markup<ul>!@foreach(var item in items) {! <li>@item.Prop</li> !}!</ul>!
  • 13. 13 Code and plain text@if (foo) {! <text>Hello world</text> !}!›  … or …@if (foo) {! @:Hello, @model.FirstName!}!
  • 14. 14 If the bounds of the variableare unclear, you can useparentheses<span>ISBN@(isbnNumber)</span>!
  • 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 Comments in Razor@*!This is a server side !multiline comment !*@!
  • 17. 17 Calling a method@(MyClass.DoSomething<Product>())!
  • 18. 18 Mixing text and expressionsHello @title. @name.!
  • 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 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