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

Thanks for your post!

شركة شحن عفش من جدة الى الامارات شركة شحن عفش من جدة الى الامارات
شركة شحن عفش من جدة الى الاردن شركة شحن عفش من جدة الى الاردن

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

Keep your casuals classy and chic with a striped one-shoulder dress with tie-up waist. Our styles in western dresses include elegant bottom wear for women – pleated pants with tie-up, high-waist flared jeans with button-down sides, embroidered skinny jeans with tassels and lots more.

Hey there, I need an Statistics Homework Help expert to help me understand the topic of piecewise regression. In our lectures, the concept seemed very hard, and I could not understand it completely. I need someone who can explain to me in a simpler way that I can understand the topic. he/she should explain to me which is the best model, the best data before the model and how to fit the model using SPSS. If you can deliver quality work then you would be my official Statistics Assignment Help partner.

I don’t have time to look for another expert and therefore I am going to hire you with the hope that I will get qualityeconomics assignment help. .Being aneconomics homework help professor I expect that your solutions are first class. All I want to tell you is that if the solutions are not up to the mark I am going to cancel the project.

That is a huge number of students. Are they from the same country or different countries? I also want your math assignment help. I want to perform in my assignments and since this is what you have been doing for years, I believe you are the right person for me. Let me know how much you charge for your math homework help services.

As much as there are discouragements, it is true that mathematics is hard. Like in my case, I was never discouraged by anyone about math but I still find it very hard and that is why I am requesting your Math assignment help. I am tired of struggling with mathematics and spending sleepless nights trying to solve sums that I still don’t get right. Having gone through your Math homework help, I am sure that I will get the right help through you. Please tell me what I need to be able to hire you.

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.

This is a fascinating and timely post. I appreciate you taking the time to write this. Continue your fantastic work! I hope to see more interesting factoids from your point of view in the future. Thank you so much for your assistance!
divorcio indiscutido sin culpa en virginia

Post a Comment