Where and how is the _ViewStart.cshtml layout file

2019-01-08 04:45发布

Here's the About.cshtml from the default MVC 3 template:

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

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

I would expect that a reference to the _ViewStart file would be found in the About.cshtml, but clearly it's not.

I've looked in global.asax and web.config, but I can't find out how the About.cshtml file is "linked" with the layout from the _ViewStart file.

Everything works as expected, I'd just like to know what's going on under the hood...

7条回答
淡お忘
2楼-- · 2019-01-08 05:47

In a more general sense this ability of MVC framework to "know" about _Viewstart.cshtml is called "Coding by convention".

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.

Wikipedia

There's no magic to it. Its just been written into the core codebase of the MVC framework and is therefore something that MVC "knows" about. That why you don't find it in the .config files or elsewhere; it's actually in the MVC code. You can however override to alter or null out these conventions.

查看更多
登录 后发表回答