这里是关于。cshtml从默认的MVC 3模板:
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Put content here.
</p>
我希望在About中可以找到对_ViewStart文件的引用。Cshtml,但显然不是。
我已经在全球范围内看过了。Asax和web。但是我不知道这个About是怎么回事。cshtml文件与_ViewStart文件中的布局“链接”。
一切都按照预期工作,我只是想知道在引擎盖下面发生了什么……
简单的回答是:
ViewStarts在渲染任何视图时首先启动。长话短说如下:
创建单个视图文件的故事:
The ViewStart is merged with ViewImports and then executed as a single file. Note that ViewImports is always merged with any cshtml file including the ViewStart file. Its purpose is to abstract @using statements and other common directives.
The output of ViewStart (such as Layout and ViewData) becomes available to the specific View file.
Inside the View file, if the Layout variable is/becomes null, the body of the view is rendered and the final output is delivered to the user.
If the Layout variable is/becomes not null, the execution is moved to the layout file which in turn is merged with ViewImports as a single file and then at the @RenderBody() statement inside the layout file execution is moved back to the view file which is merges with ViewImports again and the output is merged with the layout file at the location of @RenderBody() and the final output is finally delivered to the user.
希望这能让您了解程序生命周期中未知的秘密中到底发生了什么。
在更普遍的意义上,MVC框架“知道”_Viewstart的能力。cshtml被称为“按惯例编码”。
Convention over configuration (also known as coding by convention) is
a software design paradigm which seeks to decrease the number of
decisions that developers need to make, gaining simplicity, but not
necessarily losing flexibility. The phrase essentially means a
developer only needs to specify unconventional aspects of the
application. For example, if there's a class Sale in the model, the
corresponding table in the database is called “sales” by default. It
is only if one deviates from this convention, such as calling the
table “products_sold”, that one needs to write code regarding these
names.
维基百科
没有什么神奇的。它只是被写进MVC框架的核心代码库,因此是MVC“知道”的东西。这就是为什么你在.config文件或其他地方找不到它;它实际上在MVC代码中。但是,您可以重写以更改或取消这些约定。
在源代码中查找这些信息比在文档中查找要好得多。
参考来自Github的MVC 6代码,我们有一些感兴趣的文件
——更新
由于源结构的变化,关于如何收集视图开始页的信息现在可以在RazorViewEngine.cs寻找“GetViewStartPages”函数中找到。
——/更新
为了回答它们是如何发挥作用的,看看RazorView,我相信(因为IView)它是绑定在MVC管道中的。这个文件有一个RenderAsync方法,可以从MVC管道中调用来呈现所请求的视图。
RenderAsync调用RenderPage和RenderLayout(注意顺序)。
RenderPage首先调用viewstart文件(注意复数,可能有多个_viewstart文件)。
所以,你所寻找的信息可以从Microsoft.AspNet.Mvc.Razor命名空间下RazorView.cs文件中的RenderViewStartAsync函数中获得。
简单的回答是:
ViewStarts在渲染任何视图时首先启动。长话短说如下:
创建单个视图文件的故事:
The ViewStart is merged with ViewImports and then executed as a single file. Note that ViewImports is always merged with any cshtml file including the ViewStart file. Its purpose is to abstract @using statements and other common directives.
The output of ViewStart (such as Layout and ViewData) becomes available to the specific View file.
Inside the View file, if the Layout variable is/becomes null, the body of the view is rendered and the final output is delivered to the user.
If the Layout variable is/becomes not null, the execution is moved to the layout file which in turn is merged with ViewImports as a single file and then at the @RenderBody() statement inside the layout file execution is moved back to the view file which is merges with ViewImports again and the output is merged with the layout file at the location of @RenderBody() and the final output is finally delivered to the user.
希望这能让您了解程序生命周期中未知的秘密中到底发生了什么。
这可能会为这个问题增加一些附加信息(2016 ala MVC4, MVC5)。
Razor引擎找到并运行_ViewStart中的代码。cshtml在任何其他代码之前,这些代码位于_ViewStart. xml所在的同一目录或子目录中。找到CSHTML。
任何视图都可以覆盖Layout属性或其任何值。
只是想我可能会添加一些信息来告诉你为什么它是_ViewStart。
如果您获得ILSpy并检查RazorViewEngine (System.Web.Mvc.dll)中的代码,您将看到代码本身引用了该名称。
你可以看到RazorViewEngine在寻找这个名称的文件:
RazorViewEngine.ViewStartFileName = "_ViewStart";