这里是关于。cshtml从默认的MVC 3模板:
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Put content here.
</p>
我希望在About中可以找到对_ViewStart文件的引用。Cshtml,但显然不是。
我已经在全球范围内看过了。Asax和web。但是我不知道这个About是怎么回事。cshtml文件与_ViewStart文件中的布局“链接”。
一切都按照预期工作,我只是想知道在引擎盖下面发生了什么……
在源代码中查找这些信息比在文档中查找要好得多。
参考来自Github的MVC 6代码,我们有一些感兴趣的文件
——更新
由于源结构的变化,关于如何收集视图开始页的信息现在可以在RazorViewEngine.cs寻找“GetViewStartPages”函数中找到。
——/更新
为了回答它们是如何发挥作用的,看看RazorView,我相信(因为IView)它是绑定在MVC管道中的。这个文件有一个RenderAsync方法,可以从MVC管道中调用来呈现所请求的视图。
RenderAsync调用RenderPage和RenderLayout(注意顺序)。
RenderPage首先调用viewstart文件(注意复数,可能有多个_viewstart文件)。
所以,你所寻找的信息可以从Microsoft.AspNet.Mvc.Razor命名空间下RazorView.cs文件中的RenderViewStartAsync函数中获得。
来自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.
还有这个。
这可能会为这个问题增加一些附加信息(2016 ala MVC4, MVC5)。
Razor引擎找到并运行_ViewStart中的代码。cshtml在任何其他代码之前,这些代码位于_ViewStart. xml所在的同一目录或子目录中。找到CSHTML。
任何视图都可以覆盖Layout属性或其任何值。
只是想我可能会添加一些信息来告诉你为什么它是_ViewStart。
如果您获得ILSpy并检查RazorViewEngine (System.Web.Mvc.dll)中的代码,您将看到代码本身引用了该名称。
你可以看到RazorViewEngine在寻找这个名称的文件:
RazorViewEngine.ViewStartFileName = "_ViewStart";
在更普遍的意义上,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代码中。但是,您可以重写以更改或取消这些约定。