ASP.NET Core recognizes two special view files, which, if present, are treated specially, as follows:
- _ViewImports.cshtml: Used to specify Razor directives that should apply to all views (@addTagHelper, @removeTagHelper, @tagHelperPrefix, @using, @model, @inherits, and @inject), as illustrated in the following code snippet:
@using Microsoft.AspNetCore.Mvc.Razor
@using My.Custom.Namespace
@inject IMyService Service
- _ViewStart.cshtml: Any code that is placed here will be executed for all views; for this reason, it is a good place for setting the global common layout (which, of course, can be overridden by each view), a common model, or base view page, as follows:
@{ Layout = "_Layout"; }
But there are other uses too, such as the following:
- Adding @using directives so that all views have access to the same namespaces
- Adding @inject...