翻译:Razor剖析之第4部分:Razor页面

Dissecting Razor, part 4: Anatomy of a Razor Page

Razor剖析之第4部分:Razor页面

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

看了大量WebPages框架下的程序集后,就会想钻进Razor页面的里面去看一下运行机制。

Razor Side

Razor引擎方面


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.

一个普通的CSHTML页面翻译为一个继承自WebPage的类。代码生成器重写基类的抽象方法Execute()来把页面渲染成为HTTP响应数据流。

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. 

除去类级指令和结构(如代码块等)(将在后面讨论),在Razor的所有普通内容都终在Execute()方法中执行。

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

Razor的普通内容共有三种类型:字面、代码块和代码嵌入块。

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.

字面内容包含所有普通的文本。Razor字面内容会编译到WriteLiteral()方法中执行,把文本作为一个已经过正确转义字符串参数。Razor引擎希望这个方法(WriteLiteral)能把参数写到页面上去。

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.

和控制结构一样,代码块包含类似@{…}的语句块。在Razor里,它与<%...%>块是等同的。一个代码块的内容的执行被设计成就像在Execute()方法中执行的一样。代码块必须包含完整的声明,否则将产生C#语法错误。

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

VBHTML页面使用@Code…End代码块替代(C#的@{…})。

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.

代码嵌入块即为@-块。@-块是Razor中同ASPX页面中的<%:…%>一种等价写法。斯科特·格思里阐述了这些块是怎么标记的。代码嵌入块传递到Write()方法中,而Write()方法负责转义HTML内容并输出。

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.

网页框架里的Write()方法(来自WebPageBase类)带有一个Object类型的参数,允许在代码嵌入块放入任何表达式。它把参数传给相应的Http实体。HtmlEncode方法将调用ToString()方法,并正确转义成HTML内容后输出。如果参数为IHtmlString,HtmlEncode()将不加转义地返回ToHtmlString()方法。

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.

可以在RazorEngineHost中配置基类、方法名和默认的命名空间。除此之外,自定义的RazorEngineHosts可以重写PostProcessGeneratedCode()方法,这样就达到了随意修改生成的代码的目的。

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.dll中的WebRazorHostFactory()方法能够读取默认的命名空间、基类和自定义的主机,这一切也只需要修改Web.config文件中的<system.web.webPages.razor>节点即可达成。

引用:http://blog.slaks.net/2011/02/dissecting-razor-part-4-anatomy-of.html

原文地址:https://www.cnblogs.com/xiaxiazl/p/2429735.html