Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why have both _ViewStart and _ViewImports? Why not one file?

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?

like image 250
Hamid Sadeghian Avatar asked Dec 02 '18 15:12

Hamid Sadeghian


People also ask

What is the purpose of the _ViewImports Cshtml 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.

Can we have multiple _ViewStart in MVC?

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.

Why is _ViewImports file used in MVC core?

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.

What is the purpose of the _layout Cshtml file in the pages shared 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.

What is _viewstart in ASP NET Core?

_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.

Where to put _viewimports file in SharePoint?

_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.

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.

Can I have multiple _viewimports for the same area?

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.


2 Answers

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.

like image 122
Shadi Namrouti Avatar answered Sep 28 '22 13:09

Shadi Namrouti


One thing that has been overlooked in the other answers is that according to the official documentation:

  • Code in the _ViewStart.cshtml file will only be run for non-layout pages.
  • Code in the _ViewImports.cshtml file will be run for both layout and non-layout pages.

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.

like image 44
Jack Scott Avatar answered Sep 28 '22 12:09

Jack Scott