Razor View Syntax

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. :)

What others have said

Requesting Gravatar... Mikael Söderström Jul 03, 2010 9:15 AM
# re: Razor View Syntax
Congratulations to the awesome announcement! :)

"The good news is there’s a lot more coming!"

:D
Requesting Gravatar... Sergio Pereira Jul 03, 2010 9:28 AM
# re: Razor View Syntax
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.
Requesting Gravatar... Tony Jul 03, 2010 9:37 AM
# re: Razor View Syntax
Stop teasing, release the bits! :)
Requesting Gravatar... Jimmy Jul 03, 2010 9:45 AM
# re: Razor View Syntax
how is this performance wise compared to aspx?
Requesting Gravatar... Steve Gentile Jul 03, 2010 9:48 AM
# re: Razor View Syntax
Will it be possible to have an existing project with default engine and start including additional pages to use Razor (within the same project) ?

Requesting Gravatar... Saas Jul 03, 2010 10:16 AM
# re: Razor View Syntax
goo info, I als read Scott's blog, good things are coming.
Requesting Gravatar... Andrew Nurse Jul 03, 2010 10:41 AM
# re: Razor View Syntax
@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.
Requesting Gravatar... Haacked Jul 03, 2010 11:23 AM
# re: Razor View Syntax
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. :)
Requesting Gravatar... lynn Jul 03, 2010 1:59 PM
# re: Razor View Syntax
Cool! Cannot what to see what you guys have been working on.
Requesting Gravatar... Barley Touch Jul 03, 2010 2:07 PM
# re: Razor View Syntax
This looks fantastic! I love the succinct syntax, and am a huge fan of templating languages moving towards more brief syntax.
Requesting Gravatar... Karl Jul 03, 2010 2:22 PM
# re: Razor View Syntax
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
Requesting Gravatar... Abe Jul 03, 2010 3:42 PM
# re: Razor View Syntax

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!
Requesting Gravatar... PreachItPhil Jul 03, 2010 9:13 PM
# re: Razor View Syntax
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.
Requesting Gravatar... Quooston Jul 04, 2010 12:11 AM
# re: Razor View Syntax
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.

:)
Requesting Gravatar... Andy Jul 04, 2010 1:41 AM
# re: Razor View Syntax
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....
Requesting Gravatar... Goran Maricic Jul 04, 2010 6:30 AM
# re: Razor View Syntax
Phil, do you have some time frame for next version of ASP .NET MVC and integration with Razor ?
Requesting Gravatar... marek Jul 05, 2010 7:00 AM
# re: Razor View Syntax
Any chance this will work with VS.NET 2008?

Thanks.
Requesting Gravatar... Shiju Varghese Jul 05, 2010 10:56 PM
# re: Razor View Syntax
This is a great thing for ASP.NET MVC developers and really love the Razor View Engine.
Requesting Gravatar... Chris Airey Jul 06, 2010 1:08 AM
# re: Razor View Syntax
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.
Requesting Gravatar... Chris Airey Jul 06, 2010 1:10 AM
# re: Razor View Syntax
I mean <%: %>
not <%# %>
Requesting Gravatar... Matt S. Jul 06, 2010 9:20 AM
# re: Razor View Syntax
Chris,

You can always do something like this to avoid HTML encoding:

@MvcHtmlString.Create(Model.UserHtml)


Basically, anything that implements IHtmlString avoids the encoding.
Requesting Gravatar... Dave Schinkel Jul 06, 2010 10:02 AM
# re: Razor View Syntax
Awesome. One big leap for VS and ASP.NET MVC.
Requesting Gravatar... Andrew Nurse Jul 06, 2010 10:23 AM
# re: Razor View Syntax
@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.
Requesting Gravatar... Brian Jul 08, 2010 7:01 AM
# re: Razor View Syntax
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?
Requesting Gravatar... scott_m Jul 08, 2010 2:43 PM
# re: Razor View Syntax
@Phil

Nice sword. That a Japanese officer's sword?

Requesting Gravatar... Mikey Jul 12, 2010 12:28 AM
# re: Razor View Syntax
Wrong! That's a Hanzō sword from Kill Bill!
Requesting Gravatar... frank / fschwiet Jul 14, 2010 9:15 PM
# re: Razor View Syntax
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.
Requesting Gravatar... Bret Ferrier (runxc1) Jul 16, 2010 8:15 AM
# re: Razor View Syntax
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.
Requesting Gravatar... haacked Jul 16, 2010 10:52 AM
# re: Razor View Syntax
@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. :)
Requesting Gravatar... Daniel Crenna Jul 17, 2010 9:46 AM
# re: Razor View Syntax
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).
Requesting Gravatar... Aaron Jul 28, 2010 7:28 AM
# re: Razor View Syntax
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
Requesting Gravatar... Adam Aug 15, 2010 8:36 AM
# re: Razor View Syntax
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.
Requesting Gravatar... Camiel Aug 24, 2010 12:52 AM
# re: Razor View Syntax
@inherits System.Web.Mvc.WebViewPage<IGrouping<string, string>>

Expected '>' after generic type argument

Is this a bug?
Requesting Gravatar... Mike Aug 29, 2010 11:31 AM
# re: Razor View Syntax
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?
Requesting Gravatar... Mike Aug 29, 2010 10:37 PM
# re: Razor View Syntax
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.

What do you have to say?

(will show your gravatar)
Please add 2 and 3 and type the answer here: