The ViewImports 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. All Razor pages in the folder hierarchy will be affected by the directives set in the _ViewImports.cshtml file.

The _ViewImports.cshtml file supports the following directives:

  • @addTagHelper
  • @inherits
  • @namespace
  • @inject
  • @model
  • @removeTagHelper
  • @tagHelperPrefix
  • @using

The @addTagHelper, @removeTagHelper and @tagHelperPrefix directives relate to the management of Tag Helpers. The @namespace directive is used to specify the namespace of the pages affected by the ViewImports file, typically MyApplication.Pages. Dependency injection is supported through the use of the @inject directive. The @model directive is used to specify the Model, although you usually apply this on a page-by-page basis. The @using directive makes other selected namespaces available to all pages in the folder hierarchy to save you having to provide fully qualified names when working with their types.

The default _ViewImports.cshtml file typically contains three directives: a @using directive specifying the default namespace of your application, a @namepace directive and an @addTagHelper directive making the Microsoft.AspNetCore.Mvc.TagHelpers library contents available to your pages:

@using MyApplication
@namespace MyApplication.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

The @namespace directive specifies the root namespace that applies to pages. By convention, it is composed from a dot-separated path to the Razor Page location with the name of the application provided as the "root" and is the same as the one applied to generated PageModel classes.

You can only have one @namespace directive per ViewImports file. Adding multiple @namespace directives to the same file will result in build errors.

You can add further @using directives to bring additional namespaces into scope:

@using MyApplication
@using MyApplication.Models
@using MyApplication.Services
@namespace MyApplication.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

There is no limit to the number of _ViewImports.cshtml files that a Razor Pages application can support. You can place additional _ViewImports.cshtml files in subfolders to either add to the top level _ViewImports.cshtml file's directives, or to override its settings. The @addTagHelper, @removeTagHelper, @inject and @using directives are additive, while the other directives override each other, the closer you get to the page. So, for example, the namespace specified in the root Pages folder will be overridden for pages in a subfolder if a different value is assigned to the @namespace directive in a _ViewImports.cshtml file in that sub-folder.

Last updated: 07/05/2021 07:44:15

On this page

© 2018 - 2022 - Mike Brind.
All rights reserved.
Contact me at Outlook.com