简体   繁体   English

WebViewPage类如何在ASP.Net MVC中转换为html?

[英]How is WebViewPage class transformed into html in ASP.Net MVC?

Until recently I thought that ASP.Net views are files (like .aspx or .cshtml) that can contain some .Net code inside themselves and are parsed with a view engine (like razor or aspx) into html files that are sent to a client's browser. 直到最近,我还认为ASP.Net视图是文件(例如.aspx或.cshtml),这些文件本身可以包含一些.Net代码,并使用视图引擎(例如razor或aspx)解析为发送到客户端的html文件。浏览器。 Later I discovered that these views are actually converted into classes that derive from System.Web.Mvc.WebViewPage class. 后来我发现这些视图实际上已转换为派生于System.Web.Mvc.WebViewPage类的类。 Apart from this I noticed some people referred to views as classes themselves. 除此之外,我注意到有些人将视图本身称为类。 So my questions are 所以我的问题是

  1. If views are parsed into a class, how does this class then render an html document? 如果将视图解析为一个类,那么该类如何呈现html文档?
  2. Is it fair to say that views in ASP.NET are classes themselves or they are more like some kind of scripts on which bases a view engine forms an actual class? 可以说ASP.NET中的视图本身就是类,还是更像某种脚本,基于它们的视图引擎构成了实际的类?

A nice description of how the process works for Razor views can be found here . 此处可以找到有关Razor视图处理过程的良好描述。

Basically the view file, in this case cshtml is first converted to a C# code file on the first request of that given page. 基本上,视图文件(在这种情况下为cshtml )首先在该给定页面的第一个请求上转换为C#代码文件。 The created class basically converts all the non-C# parts of the cshtml to C#. 创建的类基本上将cshtml所有非C#部分cshtml为C#。 Mostly it is just writing out HTML literals as the response in the Execute method of the view. 通常,它只是在视图的Execute方法中写出HTML文字作为响应。

You can also see that the generated C# classes use #line directives to mark the lines of the original csproj file so that you can use the debugger to step through the original code and see where exceptions were thrown. 您还可以看到生成的C#类使用#line指令来标记原始csproj文件的行,以便您可以使用调试器单步执行原始代码并查看在何处引发了异常。

So yes, views are basically classes (at least after they are first accessed they become classes) and the rendering is caused by writing the HTML literals inside the Execute method of the view to the response stream. 因此,是的,视图基本上是类(至少在首次访问它们之后才成为类),并且呈现是通过将视图的Execute方法内的HTML文字写入响应流而引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM