Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming _ViewStart.cshtml

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.

like image 962
Humberto Avatar asked Mar 30 '11 17:03

Humberto


People also ask

What is _viewstart cshtml file in MVC?

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.

Where is the layout page specified in the viewstart file?

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.

How do I create a cshtml file in Visual Studio Code?

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.

Where are the views in an MVC project?

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.


1 Answers

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.

like image 115
Buildstarted Avatar answered Nov 12 '22 22:11

Buildstarted