In ASP.NET Core MVC we can put a file with the exact name of _ViewStart.cshtml
inside a folder to contain the common C# code to be run before every razor view/page in that folder. Something like this:
@{
const string SomeConstant = "some value";
}
Similarly a file with the exact name of _ViewImports.cshtml
inside a folder can hold all the common razor directives to be shared among the razor views/pages in that folder. Like this:
@layout _Layout
@using MyApp.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
But here's a question that I couldn't google, no matter how I rephrased it:
Can somebody please explain to me why we have both a _ViewStart.cshtml
and a _ViewImports.cshtml
to define common code & directives? Why aren't these functionalities (which don't seem to be conflicting with each-other) defined in a single file?
The purpose of the _ViewImports. cshtml file is to provide a mechanism to centralise directives that apply to Razor pages so that you don't have to add them to pages individually. The default Razor Pages template includes a _ViewImports. cshtml file in the Pages folder - the root folder for Razor pages.
We can also create multiple _ViewStart. cshtml pages. The file execution is dependent upon the location of the file within the folder hierarchy and the view being rendered. The MVC Runtime will first execute the code of the _ViewStart.
The _ViewImports. cshtml file is responsible for setting up namespaces that can be accessed by the views in your MVC 6 project, which was previously done by the web. config file in the views folder.
_Layout.cshtml This is used for common layout in web pages; it is like master page. The layout typically includes common user interface elements such as the app header, navigation or menu elements, and footer. Common HTML structures such as scripts and stylesheets are also frequently used by many pages within an app.
_ViewStart.cshtml It's a special file in ASP.NET Core MVC. The code in this file is executed before the code in an individual view is executed. Instead of setting the Layout property in each individual view, we can move that code into the _ViewStart.cshtml file.
_ViewImports.cshtml file is usually placed in the Views folder. It is used to include the common namespaces so we do not have to include them in every view that needs those namespaces. Right click on view folder and _ViewImports.cshtml file similar to _Layout.cshtml file but this file should be placed outside shared folder.
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.
Yes, you can have multiple _ViewImports.cshtml, for example, under the regular Views folder, as well as under the Views folder in Areas. They can of course import what you only need for the subset. In my case, when a view from the area is rendered, it will look for the _ViewStart.cshtml and _ViewImports.cshtml defined for that area first.
The _ViewStart file
It is used to set up shared-memory (public static variables) across all view files.
For example, the common practice for ViewStart is to set up a default value that you can override for the Layout and the ViewData / ViewBag dictionary.
The _ViewImports file
In this file you can summarize (abstract) all using statements that you commonly use in all your views.
Why to use _ViewImports file for common "using directives" instead of ViewStart?
Because using directives has the scope of the body of the current view file. So, putting @using statements inside ViewStart file won't make them available for any other view file except the body of the viewStart file itself. Therefore, comes the special ViewImports file which is designed to serve this scope extension purpose of the @using statements and other useful things, such as the tag helper, which without this special file, would be repeated inside each view file which violates the DRY (Don't Repeat Yourself) Principle.
One thing that has been overlooked in the other answers is that according to the official documentation:
I've tested this by moving the default Application Insights JavaScript snippet (the code below) from the imports file to the start file and it causing a build error on my layout page as it can no longer find the defined variable JavaScriptSnippet
.
The code I moved:
@inject Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet JavaScriptSnippet
Given this, the difference between the files is probably 'code I want to run everywhere' vs 'code I want to only run for full views', similar to the difference between .bashrc
and .bash_profile
.
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