liveBook · Manning

_ViewImports.cshtml

As explained in Pro ASP.NET Core 7, Tenth Edition

A Razor view imports file, typically _ViewImports.cshtml, declares namespaces and other directives available to Razor views in its scope.

_ViewImports.cshtml holds Razor directives shared by views or pages. Put it in Views for MVC controller views, or in Pages for Razor Pages. It saves repeating namespace imports and tag-helper registration in individual .cshtml files. The leading underscore distinguishes this supporting file from a view returned to the user. section 21.3.1 section 25.2.2

Add the directives needed by the views or pages:

  • @using imports a namespace so a view can write @model Product instead of @model WebApp.Models.Product. It also keeps other Razor type references shorter. section 21.3.1
  • @namespace sets the namespace of generated Razor Page classes. When that namespace matches the page model class, @model IndexModel can replace its fully qualified name. section 23.4.1
  • @addTagHelper registers tag helpers. Its arguments select helper classes, optionally with a wildcard, and name their assembly. Add it to one .cshtml file for that file alone, or to _ViewImports.cshtml for wider availability. section 25.2.2

A Pages imports file

In Pages/_ViewImports.cshtml, use @namespace WebApp.Pages, @using WebApp.Models, and @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers. A page can then declare @model IndexModel without its namespace and use the built-in partial tag helper to include a shared view. Restart the application and request /index to check the page; shortening the model name does not change its response. section 23.4.1 section 23.5.2

Keep Views and Pages imports separate when an application uses both MVC and Razor Pages. To make project-defined tag helpers available in both, add @addTagHelper *, WebApp to each imports file; the built-in helpers use Microsoft.AspNetCore.Mvc.TagHelpers as the assembly name. Do not confuse imports with _ViewStart.cshtml: view start selects a default layout, while imports supplies directives. section 25.2.2 section 7.1.6

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through file placement, three directives, and their effects on type names and tag-helper availability; leaves tag-helper implementation to separate treatment.
Examples
Uses Views and Pages imports files, a shortened @model declaration, a partial-view tag helper, and project-defined view-component tag helpers.
What it assumes
Assumes familiarity with C# namespaces and .cshtml files, while showing where to create the imports file and what its directives do.
What you can do after
Operate/configure it to shorten Razor type references and enable built-in or application tag helpers across the intended views or pages.
How it connects
Connects Razor views, Razor Pages, view models, page models, tag helpers, partial views, and view components.

Also explained in

Compare how 2 books explain _ViewImports.cshtml →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.