Is it possible to configure Razor to look for "master" layout files named other than _ViewStart.cshtml
? I mean, without explicitly setting the Layout
property in the content templates.
In the project I'm working on, each area has its own _ViewStart.cshtml
. On top of them there's a "global" _ViewStart.cshtml
. This works fine, but I think it would be more convenient if I could assign different names for each master template, like calling the "global" template _GlobalViewStart.cshtml
. That way it would be easier to spot each file when there are many of them open on the Visual Studio ribbon. (Silly? Perhaps.)
If it's possible, how can one do it?
Thanks.
To solve the above problems we need to use the _ViewStart.cshtml file. What is _ViewStart.cshtml file in ASP.NET Core MVC Application? In ASP.NET Core MVC Application, the _ViewStart.cshtml file is a special file and the code present in this file is going to be executed before the code in an individual view is executed.
The above Index view uses MyLayout.cshtml view which we specified within the ViewStart File which is present inside the Home Folder. So, here the layout page which is specified in the ViewStart file in the Home sub-folder overwrites the layout page specified in the ViewStart file in the Views folder.
In general, the _ViewStart.cshtml files are created within the Views or within the subfolder of the Views folder. To create “_ViewStart.cshtml” file right click on the Views folder and then select “Add – New Item” option from the context menu, this will opens the “New Item” window.
For example: If we observe the views folder of an MVC project, we will see _Viewstart.cshtml in the folder. Thus, the views in Home and OpenAccess will be rendered along with the UI in _Viewstart.cshtml.
It's hard coded in the RazorViewEngine
as "_ViewStart" so I doubt it unless you compile your own version.
public class RazorViewEngine : BuildManagerViewEngine {
internal static readonly string ViewStartFileName = "_ViewStart";
}
You could define your own base View that changes Layout to whatever you want so you don't have to modify it on every view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With