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

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

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

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

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

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


当前回答

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.

其他回答

来自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.

还有这个。

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

只是另一个想法。

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

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

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

在更普遍的意义上,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代码中。但是,您可以重写以更改或取消这些约定。