The ViewImports File

The purpose of the _ViewImports.cshtml file is to provide a mechanism to make directives available to Razor pages globally 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 heirarchy 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 that the ViewImports belongs to, typically MyApplication.Pages. Dependency injection is supported through the use of the @inject directive. The @model directive is used to specify the Model, and the @using directive makes other selected namespaces available to all pages in the folder hierarchy to save you having to provide fully qualified names.

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 to be used by pages (and their models). 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". Having the @namespace declaration in the ViewImports file saves you having to fully qualify the model names in the @model directive of individual pages.

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

The 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 sub-folders 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 eachother, the closer you get to the page. So, for example, the model specified in the root Pages folder will be overriden for pages in a sub-folder if a different @model directive is specified in _ViewImports.cshtml file in that sub-folder.

Last updated: 31/08/2018 11:30:34

On this page

© 2018 - Learn Razor Pages.
All rights reserved.