Razor View Syntax

asp.net, asp.net mvc, razor 0 comments suggest edit

UPDATE: Check out my Razor View Syntax Quick Reference for a nice quick reference to Razor.

There’s an old saying, “Good things come to those who wait.” I remember when I first joined the ASP.NET MVC project, I (and many customers) wanted to include a new streamlined custom view engine. Unfortunately at the time, it wasn’t in the card since we had higher priority features to implement.

Well the time for a new view engine has finally come as announced by Scott Guthrie in this very detailed blog post.

Photo by "clix"
http://www.sxc.hu/photo/955098

While I’m very excited about the new streamlined syntax, there’s a lot under the hood I’m also excited about.

Andrew Nurse, who writes the parser for the Razor syntax, provides more under-the-hood details in this blog post. Our plan for the next version of ASP.NET MVC is to make this the new default view engine, but for backwards compatibility we’ll keep the existing WebForm based view engine.

As part of that work, we’re also focusing on making sure ASP.NET MVC tooling supports any view engine. In ScottGu’s blog post, if you look carefully, you’ll see Spark listed in the view engines drop down in the Add View dialog. We’ll make sure it’s trivially easy to add Spark, Haml, whatever, to an ASP.NET MVC project. :)

Going back to Razor, one benefit that I look forward to is that unlike an ASPX page, it’s possible to fully compile a CSHTML page without requiring the ASP.NET pipeline. So while you can allow views to be compiled via the ASP.NET runtime, it may be possible to fully compile a site using T4 for example. A lot of cool options are opened up by a cleanly implemented parser.

In the past several months, our team has been working with other teams around the company to take a more holistic view of the challenges developing web applications. ScottGu recently blogged about the results of some of this work:

  • SQLCE 4 – Medium trust x-copy deployable database for ASP.NET.
  • IIS Express – A replacement for Cassini that does the right thing.

The good news is there’s a lot more coming! In some cases, we had to knock some heads together (our heads and the heads of other teams) to drive focus on what developers really want and need rather than too much pie in the sky architectural astronomy.

I look forward to talking more about what I’ve been working on when the time is right. :)

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

41 responses

  1. Avatar for ShakeIt
    ShakeIt July 2nd, 2010

    Congratulations to the awesome announcement! :)
    "The good news is there’s a lot more coming!"
    :D

  2. Avatar for Sergio Pereira
    Sergio Pereira July 2nd, 2010

    Great stuff Phil. It's amazing how much more pleasant ASP.NET development just became in the last couple of weeks. It's like a storm of goodness.

  3. Avatar for Tony
    Tony July 2nd, 2010

    Stop teasing, release the bits! :)

  4. Avatar for Jimmy
    Jimmy July 2nd, 2010

    how is this performance wise compared to aspx?

  5. Avatar for Steve Gentile
    Steve Gentile July 2nd, 2010

    Will it be possible to have an existing project with default engine and start including additional pages to use Razor (within the same project) ?

  6. Avatar for Saas
    Saas July 2nd, 2010

    goo info, I als read Scott's blog, good things are coming.

  7. Avatar for Andrew Nurse
    Andrew Nurse July 2nd, 2010

    @Jimmy
    Like ASPX, Razor pages are compiled to DLLs, so once you've hit the page once, it's just as fast as any other page. Razor pages also fully support Precompilation. The Razor parser is not quite as performant as the ASPX parser, at least in the beta we'll be releasing soon. After all, ASPX is a very simple language to parse. Having said that, the perf difference between Razor and ASPX is very reasonable at the moment and we haven't really done any performance tuning yet, so I'd expect the first-hit performance to be very comparable to ASPX.
    @Steve Gentile - Yep, Razor can easily co-exist with ASPX.

  8. Avatar for Haacked
    Haacked July 3rd, 2010

    Be sure to check out comments in Scott's blog post as many of these questions are answered there. I'm starting to think we should produce a FAQ on this. :)

  9. Avatar for lynn
    lynn July 3rd, 2010

    Cool! Cannot what to see what you guys have been working on.

  10. Avatar for Barley Touch
    Barley Touch July 3rd, 2010

    This looks fantastic! I love the succinct syntax, and am a huge fan of templating languages moving towards more brief syntax.

  11. Avatar for Karl
    Karl July 3rd, 2010

    An improved view engine was much needed, thanks. Spark is great, but new approaches are always welcomed.
    SQLCE and IIS Express are very poor and very late solutions to what other stacks provide. Web development on Windows is started to get dilapidated

  12. Avatar for Abe
    Abe July 3rd, 2010

    n ScottGu’s blog post, if you look carefully, you’ll see Spark listed in the view engines drop down in the Add View dialog. We’ll make sure it’s trivially easy to add Spark, Haml, whatever, to an ASP.NET MVC project. :)


    That is so exciting! Woohoo! It's about time. I love the Spark view engine and was hoping that there would be better built-in support.
    This makes my day!

  13. Avatar for PreachItPhil
    PreachItPhil July 3rd, 2010
    what developers really want and need rather than too much pie in the sky architectural astronomy.


    There's quite a laundry list of MSFT teams and people that could really use some of that focus. Far too many teams and L65+ seem to take 'make big bets' as 'make large, overly complex frameworks designed to be all things to all people'.
    It's great to see the Gu org continue to do great work like this. For shareholders everywhere, let's hope this kind of focus eventually becomes the rule instead of the exception it is today.

  14. Avatar for Quooston
    Quooston July 3rd, 2010

    Happy to see some positive feedback here after the nonsense tweeted by some irresponsible leaders in our development community.
    Well done guys, this is good news - the framework is evolving and it's great to see MS doing it's duty in that regard. Can't wait to play with the bits.
    :)

  15. Avatar for Andy
    Andy July 3rd, 2010

    I'd be interested to see ASP.NET MVC extensions supported by Microsoft that cover a number of standards like OAuth, Portable Contacts and Activity Streams. Embedding some of the lighter weight social concepts into the frameworks could really foster developer innovation.
    PS. Razor looks very expressive and cool....

  16. Avatar for Goran Maricic
    Goran Maricic July 3rd, 2010

    Phil, do you have some time frame for next version of ASP .NET MVC and integration with Razor ?

  17. Avatar for Marek Stachura
    Marek Stachura July 4th, 2010

    Any chance this will work with VS.NET 2008?
    Thanks.

  18. Avatar for Shiju Varghese
    Shiju Varghese July 5th, 2010

    This is a great thing for ASP.NET MVC developers and really love the Razor View Engine.

  19. Avatar for Chris Airey
    Chris Airey July 5th, 2010

    Looks good. I have a question about HTML encoding, if @ automatically HTML encodes is there an alternative that won't encode? I think the # symbol would have been better as that's how you would do an automatic encode using <%# %>. None the less, I think Razor really simplifies writing code in views.

  20. Avatar for Chris Airey
    Chris Airey July 5th, 2010

    I mean <%: %>
    not <%# %>

  21. Avatar for Matt S.
    Matt S. July 5th, 2010

    Chris,
    You can always do something like this to avoid HTML encoding:
    @MvcHtmlString.Create(Model.UserHtml)

    Basically, anything that implements IHtmlString avoids the encoding.

  22. Avatar for Dave Schinkel
    Dave Schinkel July 5th, 2010

    Awesome. One big leap for VS and ASP.NET MVC.

  23. Avatar for Andrew Nurse
    Andrew Nurse July 5th, 2010

    @Chris Airey - We made a conscious decision NOT to provide a non-encoding version of "@" in order to reduce scripting and other HTML-encoding-related issues. You can still avoid encoding by passing an object implementing IHtmlString to the "@" operator. All of our helpers do this, and we're looking at providing a helper to take a regular string and wrap it in an IHtmlString.

  24. Avatar for Brian
    Brian July 7th, 2010

    This looks great and I can't wait to get the bits to play around with in my MVC apps.
    I'm also thinking that a byproduct of your work making this testable also makes it an easy to use html templating engine in general. One scenario that would definately make my job a little easier would to use razor as a templating engine for generating email notifications.
    How trivial would it be to use razor in that manner?

  25. Avatar for scott_m
    scott_m July 8th, 2010

    @Phil
    Nice sword. That a Japanese officer's sword?

  26. Avatar for Mikey
    Mikey July 11th, 2010

    Wrong! That's a Hanzō sword from Kill Bill!

  27. Avatar for frank / fschwiet
    frank / fschwiet July 14th, 2010

    Razor looks nice, looking forward to MVC 3. I hope you can do the change where views can be bundled in a assembly for reuse, but Razor looks like reason enough to get the next version out the door.

  28. Avatar for Bret Ferrier
    Bret Ferrier July 15th, 2010

    So I thought you hated it when others told you that they were working on something cool but couldn't tell you about it.... So why do you plug that at the end of your post.

  29. Avatar for haacked
    haacked July 15th, 2010

    @Brett because I'm a hypocrite and I hate myself. :)
    Actually, I hate it when they do it on Twitter. Or in a blog post that offers nothing else of value.
    At least in this case, it was in the context of a blog post that actually talked about what I'm working on and I'm just hinting there's a bit more.
    See the difference?
    If not, then let's go with the I'm a hypocrite answer. :)

  30. Avatar for Daniel Crenna
    Daniel Crenna July 16th, 2010

    How suitable would Razor be for non-eval templates? We are still missing a way to surface markup to end-users without compromising servers. What I'm thinking is the ability to strip out scripting and other loopholes similar to Shopify's Liquid project (liquidmarkup.org).

  31. Avatar for Aaron
    Aaron July 27th, 2010

    I'm loving the Razor syntax. I just noticed something which is a speedbump for me.
    @using (Html.BeginForm()) {
    <div>
    @Html.LabelFor(x => x.UserName)
    </div>
    <div>
    @Model.UserName
    </div>
    }
    -- becomes --
    <form action="/Home/Test"> <div>
    <label for="UserName">User Name
    </div>
    <div>
    Name
    </div>
    </form>
    ...it seems to get it partially right. The @Model.UserName behaves exactly as I'd expect. I am concerned that with a long <form> tag, the <div> will be off-window to the right. In this case, not a huge deal, but with complex html layout it would become a speedbump in tracking down a rendering issue.
    Either way, I love the new Razor syntax. Thanks for bringing it to us!
    Aaron

  32. Avatar for Adam
    Adam August 14th, 2010

    Good work, really liking Razor... Except...
    I think outside of @{ } if statements should expect tags between the scopes..
    Say I have a variable called @test
    // The following
    @if (true) {
    @test
    }
    // would return my variable to the screen...
    @if (true) (
    test
    }
    // would return the text test to the screen...
    I really don't like the <text></text> syntax, and it's impeding if your mixing XML in, say for an AJAX or RSS service and you have a <text> element.
    I know Razor is for HTML... It's also for XHTML, and mixing XML is a valid thing to do. If it could do XML too (even XML with <text> elements,) that'd be really cool.

  33. Avatar for Camiel
    Camiel August 23rd, 2010

    @inherits System.Web.Mvc.WebViewPage<IGrouping<string, string>>
    Expected '>' after generic type argument
    Is this a bug?

  34. Avatar for Mike
    Mike August 29th, 2010

    I am getting the same thing as Camiel with this:
    @inherits System.Web.Mvc.WebViewPage<DateTime?>
    Expected '>' after generic type argument
    This works fine in the webforms engine. Is this a bug? Any way around it?

  35. Avatar for Mike
    Mike August 29th, 2010

    If you are running into this issue,the Razor parser seems to be having issues with nullable types. I switched my references from int? and DateTime? to System.Nullable<int> etc. That fixed the issue for now.

  36. Avatar for Kevin
    Kevin September 12th, 2010

    Phil,
    I'm loving Razor! Syntatically, it's very clean.
    Two questions:
    1) Where's the best place to post feedback?
    2) I've ran into an issue with tags spanning conditionals. Is this an enforced limitation?
    For example, I would expect the following to emit "this should be bold</b", but instead I get " } this should be bold if(true) { "

    @if(true) { }
    this should be bold
    if(true) {
    }

  37. Avatar for Kevin
    Kevin September 12th, 2010

    :) That last example got encoded. I'll restate, using '[' and ']' instead of greater-than, less-than signs.
    I would expect the following to emit "[b]this should be bold[/b]", but instead I get "[b] } this should be bold if(true) { [/b]"

    @if(true) { [b] }
    this should be bold
    if(true) { [/b] }

  38. Avatar for haacked
    haacked September 13th, 2010

    I believe this is a known bug and will be fixed in the next drop. Thanks Kevin.

  39. Avatar for Guest
    Guest September 28th, 2010

    Camiel - a workaround is to use dynamic instead, eg:
    @inherits System.Web.Mvc.WebViewPage<dynamic>

  40. Avatar for marius
    marius October 4th, 2010

    hi phil,
    is there a way to specify the &lt;% Assembly Name="xxx" &gt; declaration in razor?!
    something like this...
    @assembly.Name="Sample"
    @assembly.Name "Sample"
    @assembly.Name Sample
    @assembly Sample
    @assembly "Sample"
    note! i don't mean the &lt;% import aka @using declaration
    (assembly not the namespace)

  41. Avatar for awesome
    awesome October 20th, 2010

    Will MVC3 support Visual Studio 2008?