BT

Your opinion matters! Please fill in the InfoQ Survey!

Follow-up: Razor with F# and Other Languages

| by Jonathan Allen Follow 186 Followers on Jan 03, 2011. Estimated reading time: 1 minute |

Last month Vladimir Kelman asked if it were possible to use F# with the new Razor view engine. After talking with Scott Guthrie and Marcin Dobosz we learned that it is possible, if you want to put in the effort to build all necessary plugins yourself.

The first challenge is the syntax. Unlike the older ASP-style view engine, the boundaries between view and inline code are quite subtle and it is quite possible that many languages are syntactically incompatible. According to Scott Guthrie, “Razor can conceptually support F# (meaning the syntax is such that it could work well)”. He then turned over the conversation to Marcin Dobosz who explained what would be necessary to actually wire it in.

On the pure Razor side of things (System.Web.Razor.dll), you would need to implement your own classes derived from CodeParser, RazorCodeGenerator, and RazorCodeLanguage (as well as any other utlity classes that are necessary) and then register your file’s extension with RazorCodeLanguage.Languages. We don’t have any tutorials for implementing your own language so you would have to look at the sources to see what the CSharp or VB-related classes do.

On the MVC side (but only if you want support for @model) you would need to derive from MvcWebPageRazorHost and return MVC-specific parser and generator that derive from your basic parser and generator. Once again take a look at the sources of classes in the System.Web.Mvc.Razor namespace. Or you could implement this in your basic parser and generator, making the whole thing only work in MVC projects.

Of course this is just the bare minimum to get Razor working with a new language. If you were going to spend the effort to write the parser and generator, you probably want to take the extra step to create the project, item, and T4 templates needed that Visual Studio needs for a pleasant experience.

The source code for the October 2010 beta of ASP.NET MVC 3 is available on Codeplex. It is released under the “Microsoft Source License for ASP.NET Pre-Release Components”.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT