Dissecting Razor, part 1: Parts of the framework

Razor involves two distinct components: The Razor engine and the WebPages framework.

The Razor engine, in System.Web.Razor.dll, parses CSHTML (and VBHTML) files into CodeDOM trees.  Except for the word Web in project name, the engine has nothing to do with ASP.Net; it doesn’t even reference System.Web.dll.  In fact, it targets the .Net Client Profile, and only references mscorlib and System.dll.

The Razor engine is aware of all of Razor’s syntax-level features (code nuggets, sections, helpers), but is not aware of what they mean; it blindly transforms them into function calls.

The Razor engine can be used without ASP.Net for any kind of templating system.  This is done by inheriting the RazorEngineHost class to provide default code-generation settings (such as base class and method name), then passing the host to a RazorTemplateParser. 

The standard syntax will be annoying when generating non-XML-like content.  To avoid this, one can write a custom MarkupParser to define a Razor syntax for a different markup language (such as CSS).

The WebPages framework, in System.Web.Webpages.dll, is a set of classes to use with the Razor parser.  It contains the WebPage class which standard Razor pages inherit. This class defines the methods which are blindly called by the Razor parser, such as DefineSection and WriteLiteral.    This framework also handles _PageStart and _AppStart pages and contains the HtmlHelper infrastructure.

The WebPages framework is not directly connected to the Razor parser.  It could theoretically be used with a different template engine, as long as the template engine emits the correct method calls and class definitions.  (more on this later)

The WebPages framework also contains two sets of utility methods.  System.Web.Helpers.dll contains miscellaneous utility classes, such as Crypto and WebMail wrappers, plus grid and chart implementations.  Microsoft.Web.Helpers.dll contains HTML helper classes which integrate with various third-party services, including Twitter, ReCaptcha, Google Analytics, and more.  Most of these helpers can also be used in ordinary ASPX pages.

The source code for all of these projects is available here.

Next time: Gluing it all together.


Hi, I don't know why you nobody post comments. Your dissection is excelent and was what I'm looking for. I'm interested in adding support for parsing CSS files, do you know if someone has done it?


@MarianoC: I assume that you mean generating CSS files.

As I mentioned in the post, you would need to write a CssMarkupParser; this class would re-implement all of Razor's markup-side features (tag nesting and transitions to server-side code, plus and @:) for CSS.

Since CSS doesn't support arbitrary nesting, this would be simpler than HtmlMakrupParser.

I don't know of anyone who has done this; if you're interested, download the source and look at mvc3-rtm-sources\webpages\src\System.Web.Razor\Parser\HtmlMarkupParser.cs.

Outstanding post, I found your blog as extremely fascinating and the composition form of your posts is pleasantly essentially based.I am helping college student through dissertation writing service, students can get tips and tricks approximately essay writing. I am really impressed on your writing, keep posting this type for short story, looking forward for your new one.

I recommend you to pay attention on information from https://nerdymates.com/blog/how-to-write-definition-essay. Here you can learn how to write definition essay

Relevant information about Razor engine. It specification and new features are really useful. Thanks for sharing this valuable points here. Best Essay Writing Service

mr jatt Mp3 New Punjabi Song,Single Tracks Latest song download also Listen Latest Music Albums Online in High Quality at Mrpendus.in
mr jatt

Hello, I would like to thank you for sharing this interesting information. The material provided here will be useful for a lot of people. I would like to share with you all as well one useful sourcehttps://writessay.org/buy-an-interview-essay.php which will be helpful for a lot of people.

I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best!
like app for pc

Need assistance with your research paper? Look no further. Help with research paper provides professional writing services to help you achieve academic success. Our expert writers will ensure that your paper is 100% original and delivered on time. Contact us today for help with your research paper.

A framework is a structured set of tools, libraries, and conventions designed to facilitate the development of software applications. It provides a foundation for building, organizing, and deploying code, streamlining the development process and promoting consistency. Frameworks often include pre-built functionalities and best practices, allowing developers to focus on application-specific logic rather than reinventing common functionalities.
contract dispute lawyer
truck accident injury lawyer

Post a Comment