这里是关于。cshtml从默认的MVC 3模板:

@{
    ViewBag.Title = "About Us";
}

<h2>About</h2>
<p>
     Put content here.
</p>

我希望在About中可以找到对_ViewStart文件的引用。Cshtml,但显然不是。

我已经在全球范围内看过了。Asax和web。但是我不知道这个About是怎么回事。cshtml文件与_ViewStart文件中的布局“链接”。

一切都按照预期工作,我只是想知道在引擎盖下面发生了什么……


当前回答

只是另一个想法。

如果您想拥有自己的cshtml文件作为公共模板,可以采用这种方式

在你的_viewstart。你可以提到你常用的CSHTML文件。

@{Layout = "~/Views/Shared/_Layout.cshtml";}

其他回答

来自ScottGu的博客:

Starting with the ASP.NET MVC 3 Beta release, you can now add a file called _ViewStart.cshtml (or _ViewStart.vbhtml for VB) underneath the \Views folder of your project: The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. For example, we could write code within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default: Because this code executes at the start of each View, we no longer need to explicitly set the Layout in any of our individual view files (except if we wanted to override the default value above). Important: Because the _ViewStart.cshtml allows us to write code, we can optionally make our Layout selection logic richer than just a basic property set. For example: we could vary the Layout template that we use depending on what type of device is accessing the site – and have a phone or tablet optimized layout for those devices, and a desktop optimized layout for PCs/Laptops. Or if we were building a CMS system or common shared app that is used across multiple customers we could select different layouts to use depending on the customer (or their role) when accessing the site. This enables a lot of UI flexibility. It also allows you to more easily write view logic once, and avoid repeating it in multiple places.

还有这个。

只是另一个想法。

如果您想拥有自己的cshtml文件作为公共模板,可以采用这种方式

在你的_viewstart。你可以提到你常用的CSHTML文件。

@{Layout = "~/Views/Shared/_Layout.cshtml";}

在源代码中查找这些信息比在文档中查找要好得多。

参考来自Github的MVC 6代码,我们有一些感兴趣的文件

——更新

由于源结构的变化,关于如何收集视图开始页的信息现在可以在RazorViewEngine.cs寻找“GetViewStartPages”函数中找到。

——/更新

为了回答它们是如何发挥作用的,看看RazorView,我相信(因为IView)它是绑定在MVC管道中的。这个文件有一个RenderAsync方法,可以从MVC管道中调用来呈现所请求的视图。

RenderAsync调用RenderPage和RenderLayout(注意顺序)。 RenderPage首先调用viewstart文件(注意复数,可能有多个_viewstart文件)。

所以,你所寻找的信息可以从Microsoft.AspNet.Mvc.Razor命名空间下RazorView.cs文件中的RenderViewStartAsync函数中获得。

If you want to have a common layout for your pages you need to define the common layout and to associate a view with layout we have to set layout property on each and every view, this violates the DRY(Don't Repeat Yourself) principle. For this .Net Framework has provide the "_ViewStart.cshtml" file, placed inside the view folder. We place layout information in "_ViewStart.cshtml" file and every view by default uses this layout information. If you want to give some different layout information, lets suppose to your Home view, you can create a new "_ViewStart.cshtml" with reference to that layout and place it in the "Home View" folder.

简单的回答是: 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.

希望这能让您了解程序生命周期中未知的秘密中到底发生了什么。