Where and how is the _ViewStart.cshtml layout file linked?
From ScottGu's blog:
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.
Also see this.
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.
Just another thought.
If you want to have your own cshtml
file as a common template, you can do it this way
Within your _viewstart.cshtml
you can mention your common cshtml
file.
@{Layout = "~/Views/Shared/_Layout.cshtml";}
The source code is a much better place to look for this than the documentation.
Referencing the MVC 6 code from Github, we have a few files of interest
----update----
Due to source structure changes, the information on how viewstart pages are gathered can now be found in RazorViewEngine.cs look for "GetViewStartPages" function.
----/update----
To answer how they come into play, look at RazorView, Which I believe (because of IView) is tied in to the MVC pipeline. This file has a RenderAsync method that gets called from the MVC pipeline to render the requested view.
RenderAsync makes calls to RenderPage AND THEN RenderLayout (NOTE THE ORDER). The RenderPage first makes calls to deal with viewstart files (note plural, there could be more than one _viewstart file).
So, the information you seek can be obtained from RenderViewStartAsync function in RazorView.cs file under Microsoft.AspNet.Mvc.Razor namespace.