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:

<system.web.webPages.razor>
    <host factoryType="MyProject.MyWebPageRazorHost" />
    <pages pageBaseType="MyProject.CustomWebPage">
        <namespaces>
            <add namespace="MyProject.SomeNamespace" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

Next Time: Looking at the generated page

17 comments:

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

Thanks for sharing this marvelous post. I m very pleased to read this article.
office.com/setup

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

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

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


Roku is the easiest way to link your Roku device with your tv and gives next level video streaming experience. roku.com/link has millions of users in the United States. After all, it’s very convenient in terms of you. You can watch more than five lacs of free and paid movies, videos, and tv shows. You only need to visit the official Roku link roku.com/link to get started with your Roku device. The Roku itself is a compact video streaming device that can merge all user’s favorite tv shows, videos, and movies together.

amazon.com/mytv is the official web link to activate amazon prime video subscription safely. Various media players are being launched nowadays. Media players support the best quality of videos. But still, they fail to provide it. So for the best video quality support, Activate Amazon my TV is being launched. Prime video is a popular platform. It gives you high-quality videos that many other media players fail to provide. Amazon mytvis a platform that shows the original quality of the video.

You can book call girls in Guwahati or escorts in Guwahati for incall facility or outcall
Facility as and when required. To book Guwahati call girls you
can visit the following links
Call Girl in Guwahati
Guwahati Call Girl

office setup home student 2019 is an all-purpose application bundle that makes presentations, calculations, and reports simple. Microsoft is an American firm, succeeds in providing the vast software package of applications for the official workplace.

This blog is very informative and is very helpful for understanding web pages..

office.com/setup

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

https://www.shoutoutnerds.com

A Detailed Guide to Fixing AirPods Not Charging Issues

There are situations when converting between data types is beneficial. The most typical illustration is changing a string input into another type, like an integer or a date. Even if the user submitted a number, user input often comes as strings. As a result, before they can be utilized in calculations, numeric input values must be transformed to numbers. You may create a loop to execute the same statements repeatedly. A for loop can be used if you know how many times you want to loop. Have some microsoft solitaire free games here.

Play Azur Lane on PC now!

This blog offers an in-depth and insightful examination of the Razor Page framework. This review effectively breaks down the components and functionality of Razor Pages, making complex concepts accessible to developers of all levels. The analysis is thorough, providing clear explanations of the page structure, data handling, and interactions within Razor Pages. It’s an invaluable resource for anyone looking to deepen their understanding of Razor Pages and enhance their web development skills.
charlottesville nursing home abuse lawyer

Post a Comment