ASP.NET Core MVC Interview Question #18

What is _ViewImports.cshtml?

MVC Fundamentals, Controllers, Views & Routing Junior Beginner

Quick Interview Answer

_ViewImports.cshtml centralizes Razor directives that should apply to views in a folder hierarchy. It commonly contains @using directives, @addTagHelper registrations, and @namespace settings.

Detailed Explanation

_ViewImports.cshtml centralizes Razor directives that should apply to views in a folder hierarchy. It commonly contains @using directives, @addTagHelper registrations, and @namespace settings.

This avoids repeating common imports in every view and keeps Razor files cleaner. It is different from _ViewStart.cshtml: ViewImports supplies directives and imports, while ViewStart executes Razor code such as layout selection.

Code Example

@using MyApp
@using MyApp.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers