More On Razor Syntax / Expressions

This post gets enough said about how the Razor parser works.,guid,89b7bd90-52d7-4d49-b87d-4e888f285b4c.aspx

The guy, Andrew Nurse, is the one who wrote the parser!   (from Haack’s note)


After reading it and a quick chat with @Haacked on twitter, it seems all your “escape” kind of Razor expressions (that is meaning: when mixing code and text without spaces etc…) will look like @(someCode)someText. This is coming from an example, to escape a C# identifier that’s also a keyword, say “class”, you’d be using @(@class). The @(…) style is the new <%: … %> but only required for escape situations.


Also, it seems that switching from being equivalent to <%: … %> to being equivalent to <%= … %> by using some Razor notation is not gonna make it. You need to do it through doing some classes implementing IHtmlString interface.


Of course I’m not so sure that’s exactly the case. Anyway, Razor is only available now in WebMatrix mini-IDE, not much MVC love yet (pretty soon).  Just thought it might be interesting…

Just In Case You Missed The New ASP.NET Betas (WebMatrix, Razor, IIS Express, SQL Server Compact, Web Platform Installer)

Similar to the last post, it looks like the best way to continue blogging for me is to copy private company/list emails (when appropriate of course). This is a mail I just sent to .NET list in my company, with slight modifications:

Hey all,

Just in case you have not noticed it already, early last night (before I woke up near midnight our time!) Microsoft released public betas of some new and fancy stuff…

1- IIS Express

Remember when I mentioned it before? A nice alternative to Visual Studio built-in dev server.

Supports SSL and other nice stuff, works even on Windows XP but simulates IIS 7.5, no admin privileges required

2- SQL Server Compact Edition

A file-based database engine, just like SQL Server Express, except that when you develop your website with it, you don’t need it to be installed on the server to get running (or anything else installed)

3- New ASP.NET Pages Syntax code-name Razor

This is a new syntax that is going to replace the old <% … %> ASP-Classic-like style we write ASPX/ASCX pages

It’s going to be mainly for ASP.NET MVC, but watch out, I smell like it’s may reach web-forms also

(It already now can work with or without MVC)

4- Microsoft WebMatrix
You may have heard the name before as a very old web tool for .NET 1.x,

No, this is a completely new Web development and deployment tool.

It installs all the previous components in it (but total small size 15 MB) and supports them out of the box

Also support Search Engine Optimization Kit for IIS Express and nice automatic Web Deployment model

Targeted at .NET beginners (typically coming from other dynamic languages I’d say based on what I see, it doesn’t have intellisense even, which is common in these languages IDEs)

5- Microsoft Web Platform Installer 3.0

The component you can use to install different web development stuff like the mentioned above (and like SQL Express, Visual Web Developer Express, and even PHP) and also read-applications like blog engines and such that you can sstart with and modify their source later

The announcement from Scott Guthrie that came yesterday can be found at:

For great tutorials see Scott Hanselman Post (Most Importantly, it has links to video and tutorials about all mentioned)

For feature-specific announcements from Scott Guthrie see:

· IIS Developer Express: A lightweight web-server that is simple to setup, free, works with all versions of Windows, and is compatible with the full IIS 7.5.

· SQL Server Compact Edition: A lightweight file-based database that is simple to setup, free, can be embedded within your ASP.NET applications, supports low-cost hosting environments, and enables databases to be optionally migrated to SQL Server.

· ASP.NET “Razor”: A new view-engine option for ASP.NET that enables a code-focused templating syntax optimized around HTML generation.  You can use “Razor” to easily embed VB or C# within HTML.  It’s syntax is easy to write, simple to learn, and works with any text editor.

Also, I wrote a detailed blog post on the very early morning yesterday about my thoughts of Razor syntax and ASP.NET MVC, and made sure the post is friendly to those who almost don’t know about ASP.NET MVC itself, find it here:

You know what? Let me just quote the list of videos and tutorials better…

· Channel 9 Video: WebMatrix with Scott Hunter and Simon Calvert

· Learn by Doing – WebMatrix walkthroughs

o 1 – Getting Started

o 2 – Coding with Razor Syntax

o 3 – Creating a Consistent Look

o 4 – Working with Forms

o 5 – Working with Data

o 6 – Working with Files

o 7 – Working with Images

o 8 – Working with Video

o 9 – Adding Email to your Website

o 10 – Adding Social Networking

o 11 – Analyzing Traffic on your Website

o 12 – Adding Caching for Faster Websites

o 13 – Adding Security and Membership

o 14 – Introduction to Debugging

o 15 – Customizing Site-Wide Behavior

o ASP.NET Web Pages API Reference

· WebMatrix Tutorials and FAQs

o WebMatrix Overview

o Create a Website from a Gallery Application

o WebMatrix Beta Release Readme

o Using WebMatrix Beta[Show All]

§ Download and Install an ASP.NET Application

§ Download and Install a PHP Application

§ Make your Website SEO Friendly

§ Analyze Your Website

o Using IIS Developer Express

§ IIS Developer Express Overview

§ Use the Windows System Tray to Manage Websites and Applications

§ Use the Command Line to Run a WebMatrix Site or Application

§ IIS Developer Express FAQ

o Application Gallery FAQs

§ Acquia Drupal FAQ

§ AtomSite FAQ

§ BlogEngine.NET FAQ

§ dasBlog FAQ

· File a Bug on WebMatrix or Suggest a Feature

Have Fun!


Update 01:

There is one more tutorial I forgot to mention

Thoughts On Razor, Microsoft’s New ASP.NET MVC View Engine

This is something I have posted to a private mailing list before, and thought since I have only fixed number of keyboard strokes to death, I should be sharing it with larger audience…

Before Beginning

imageI know some of the audience of this blog may have not even tried ASP.NET MVC, so, you may need to bare with me for a while ((and those familiar with it just bypass this section please).

In ASP.NET MVC, the request goes to a specific method (commonly known as Controller Action) to handle it (choosing which method/action is based on something called Routing, we don’t care about that for now).

Once the method is executed, typically it ends with calling a page or user control (commonly called a View) to send some markup to the browser. Usually this is an ASPX or ASCX file without code behind. It has some special properties to interact with the data coming from the controller action, and some special shortcut methods to write HTML markup (called HTML helpers).

This page can have a master page, include other user controls, and even have a <form runat=server if you want, however, someone may shoot you for doing it in MVC view, because no body uses server controls in MVC. All is using plain HTML/JS (with some helpers to speed the process). Typically you end up having a page that looks very similar to to ASP classic pages, with so many <%.. %> and <%= … %> inside this HTML though, and -depending on how you do it- it can get really ugly.

On View Engines…

Of course, this is not the only way to write the view markup in ASP.NET MVC. ASP.NET MVC enables something called “View Engine”. This View Engine handles finding the view file (whether it is a shared file like master page or .ASPX page or even partial like user control), parsing this view file, and executing any special code parts in it (like the parts inside <%.. %> and <%= … %>). This is not a new idea in ASP.NET MVC, as it existed before in Ruby On Rails and the open source .NET MVC Framework MonoRail.

The community then has taken care of doing alternative view engines. This means ability to write the same markup with different syntax than the ASP.NET syntax we all know (which became known as Webforms View Engine because it’s the same like in web forms development).

Some of there were ported from other MVC frameworks like MonoRail, and some were completely new. Some examples are NVelocity, NHAML, and Spark, which is created especially for ASP.NET MVC and later supported other .NET MVC frameworks like FubuMVC and MonoRail, It tries to make all the code look like html where loops and such can be added as custom HTML tags or attributes. It is also getting the most love from ASP.NET MVC developers not using the default view engine. Go to Spark homepage to get a sense of how the syntax can be different.


After Scott Guthrie, Microsoft’s Most popular Vice President, blogged about the new coming view engine from Microsoft itself code-name Razor, and showed some samples of how your view might look like using it (stating clearly that the webforms view engine will then be obsolete),  this brought many interesting points…

  • There is coming a built in extensibility support for having custom view engines integrated with Visual Studio dialogs. This is great to know, especially if you you see how people suffer to implement some support with the current tooling.

  • Part of this tooling you’ll be able to have multiple view engines set by file extension| for example, enabling you to have multiple view engines in the same project easier. This is typically something you don’t need 90% of the time, but when the rare time comes and you need it, it should be quite handy.



Razor beta is not even out. All we know about it is from different blogs and twitter conversations. Everything written here may be invalid to some point in the future or completely incorrect.


imageThere was a public discussion on twitter between Rob Conery (ex-Microsoft ASP.NET MVC team member, now runs his own business, Tekpub, a GREAT technical videos learning site) and Phil Haack (A popular Program Manager in Microsoft ASP.NET team, mainly focused on ASP.NET MVC, also founder of Subtext, my favorite open source .NET blog engine) about why Razor, why not just give even more VS love to Spark being a very popular ASP.NET MVC view engine (and given the creator of Spark is now a member of ASP.NET team also!). In this conversation Rob said he is more convinced about the decision after a phone call from Phil and some interesting comments (will mention one of them below) were simply deleted after that, as the guys in the private list made me notice. Politics :)

I also got involved in talking to Phil and others about it, but this is another (way much smaller) story…

Finally Getting to the Thoughts…

Mainly the private mail conversation was about Razor, the twitter conversation, and why Microsoft seems to clone open source projects rather than just highlight them to its customers.  Reminded me with something I retweeted few days earlier:

RT @tehlike:RT @hconceicao:RT @chriso:microsofts desire to clone OSS needlessly raises the adoption cost for all other OSS in .NET ecosystem


imageI’ll not quote anybody but myself of course:. Usually I’m too lazy when copying things from my mail box, and I think the message is self explanatory now, so, I’ll just quote it and maybe update it later…

You made me notice that @Haacked tweet about jquery is now deleted :D

In case you have been wondering, I mean when he was answering Rob Conery about why Razor, saying that he asked the same guys who started jQuery why not just use prototype.js for their stuff. That was tough!

I understand the case with NHibernate because where it originates from (The Java/JBoss roots, even though they’re not the main director of NH at least now). They had to come with something they own.

I think the jQuery support itself just came because it became the defacto standard all over the web (.NET and others). And because it made their own lives either (developing MS AJAX stuff as jQuery plugins).

But the default is that unless they can acquire it completely (which does not seem to work for OSS stuff for some reason), or they come with their own property. Because they cannot support others. Especially open source. They aim at customers that seek “professional support” here and they want to secure themselves when committing to such.

For Razor itself, I like the cleaner web forms thing, but very scared that they may or may not get this mixed-html-code thing right (don’t think it’s easy, tell me all the dynamic kids are doing so, but still.. I’m not sure MS will do it right). Also I hate “@”, even more, I hate having to escape “@” with “@@” if followed by some text that the parser can understand as code. This is really ugly. Much love the Spark ${ } style, but they won’t do something similar, as they want to escape { } / < > / <% %> open/close style. Just hope they change “@” to something less common and get the parser right.

There is a website for HTML templating stuff I’m building for a friend for some time now (paused as the friend had a higher priority project for me now). People are uploading HTML files that include Spark syntax (against a defined model with user friendly property names) and I created a couple of helper classes to read the files, get Spark engine to translate them.

It was very easy for the test designers to get started with it and they loved it that a friend designer wanted all templating stuff (email templates, etc..) in the current active project we’re doing together to use Spark also. I will probably use Razor for MVC stuff, but not abandon Spark as a great templating engine.


Spotting the Positives / New Stuff

Not sure whether this part should have come first, but to be fair, there are some good “NEW” parts expected to come with Razor. I have referred one on twitter in a reply to @Orangy (the main guy behind Resharper, the must-have plug-in for all Visual Studio Versions).

Quoting from Phil HAack’s post on 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.

Also, Scott Guthrie put a comment reply to his own post about why Razor may be better for unit testing your views (if you think you want to):

The Razor parser and view engine can be instantiated and used outside of the ASP.NET application domain.  This means you can directly instantiate and use it within a unit test project without any dependencies on running ASP.NET. 

From a unit testing perspective you can indicate the view template you want to run, supply it with any dependencies, and then pass your own test models/viewmodels to it and have it run and render back a string to you. You could then verify that the correct content came back.  This would isolate the views from your controllers and any data access, and allow you to also isolate them from the runtime environment.  View engines in ASP.NET MVC VNext (both Razor and the .ASPX one) will also support and integrate with dependency injection as well.

Different people have differing opinions about the usefulness of verifying the HTML that comes back from a view using unit tests.  For scenarios involving a lot of javascript something like browser based testing is probably better.  But for basic coverage it can be useful.  It also helps verify that you don’t have compile errors or other runtime boundary errors with your views.


It’s worth mentioning that the guy writing the Razor parser seems to be a really smart fun guy. See his own writing on the parser.

Other (Better) Notes I Have Read

Some of the people who shared similar notes and I agree with (to different extents)…


So, now, what are your own thoughts?