Dissecting Razor, part 4: Anatomy of a Razor Page

After looking at the various assemblies in the WebPages framework, we will drill into the inner workings of Razor pages.

Razor Side

An ordinary CSHTML page is transformed into a class which inherits the WebPage class.  The generator overrides the abstract Execute() method from the to render the page to the HTTP response stream.

Except for class-level directives and constructs (which will be discussed later), all ordinary content in a Razor page end up in the Execute method. 

There are three types of normal content: Literals, Code Blocks, and Code Nuggets.

Literals include any normal text. Razor compiles literal text into calls to the WriteLiteral method, with the text as a (correctly-escaped) string parameter.  Razor expects this method to write its parameter to the page.

Code Blocks include @{ ... } blocks, as well as control structures.  They’re Razor’s equivalent of <% ... %> blocks.  The contents of a code block are emitted as-is in the Execute method.  Code blocks must contain complete statements, or they’ll result in C# syntax errors.

VBHTML pages use @Code ... End Code blocks instead.

Code Nuggets are @-blocks.  They’re Razor’s equivalent of <%: ... %> blocks in an ASPX page.  Scott Guthrie describes how these blocks are tokenized.  The contents of a code nugget are passed to the Write method, which is expected to HTML-escape its parameter and print it.

WebPages Side

The WebPages framework’s  Write method (which comes from the WebPageBase class) takes a parameter of type Object, allowing one to put any expression in a code nugget.  It passes its parameter to HttpUtility.HtmlEncode, which will call ToString() and HTML-escape the output.  If the parameter is an IHtmlString, HtmlEncode will return its ToHtmlString() method without escaping.

The base class, method names, and default namespaces can be configured in the RazorEngineHost.  In addition, custom RazorEngineHosts can override the PostProcessGeneratedCode method to make arbitrary modifications to the generated code.

The WebRazorHostFactory in System.Web.WebPages.Razor.dll can also read default namespaces, a default base type, and a custom host from the <system.web.webPages.razor> section in Web.config:

    <host factoryType="MyProject.MyWebPageRazorHost" />
    <pages pageBaseType="MyProject.CustomWebPage">
            <add namespace="MyProject.SomeNamespace" />

Next Time: Looking at the generated page


These tips are nice to read. Bella

I know that after reading information from https://order-essay-online.net/blog/essay-rhetorical-devices you will learn more about essay rhetorical devices. I had such experience recently

McAfee Activate – Get robust protection by downloading and installing the McAfee antivirus. Go to McAfee Activate and enter the product key to activate your McAfee product #mcafee #activate mcafee.com/activate | mcafee.com/activate | mcafee.com/activate | mcafee.com/activate

Thank you so much for sharing such superb information's with us. Download the Microsoft Office setup on your Microsoft application provides a better way to access information, and consequently, users can have greater insight, and they can manage most of their essential work with more productivity and in less time. office.com/setup mcafee.com/activate|office.com/setup

Thanks for sharing this marvelous post. I m very pleased to read this article.

الرياض الى مصر نقل عفش من الرياض الى مصر
نقل عفش بالدمام نقل عفش بالدمام
نقل عفش بالاحساء نقل عفش بالاحساء
نقل عفش داخل مكة نقل عفش داخل مكة

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

There are many online environmental health research paper writing services and Environmental Health Writing Services to choose from for those stuck with their environmental health essay writing services and environmental health assignment writing services.

I have kept your website. It is illuminating and fulfilling for me. I regard the central information you offer on your site. A devotion of thankfulness is all together for posting it.
helo app download for pc

Post a Comment