Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Follow-up: Razor with F# and Other Languages

Follow-up: Razor with F# and Other Languages

This item in japanese

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