ViewImports in ASP.NET Core MVC

Introduction :

In This tutorial we are learn about how to use ViewImports in ASP.NET Core MVC, The purpose of the _ViewImports.cshtml file is to provide a mechanism to centralize directives that apply to Razor pages/views so that you don’t have to add them to pages/views individually.

In ASP.NET Core MVC Application, the _ViewImports.cshtml file provides a mechanism to include the directives globally for Razor Pages/Views so that we don’t have to add them individually in each and every page/views. As of this article, the _ViewImports.cshtml file supports the following directives.

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

The @addTagHelper,@tagHelperPrefix and @removeTagHelper 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.

Dependency injection is supported through the use of the @inject directive.

The @using directive makes other selected namespaces available to all pages/views in the folder hierarchy to save you having to provide fully qualified names when working with their types.

The @model directive is used to specify the Model, although you usually apply this on a page-by-page basis.

The default _ViewImports.cshtml file typically contains three directives:

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

ViewImports in ASP.NET Core MVC


Let us take a simple example to see how to move our using directives into ViewImports. Inside the Index view, we have a using directive to bring in the namespace DotNetCoreRepository.Models as shown in the following program.

@using DotNetCoreRepository
@model IEnumerable<DotNetCoreRepository.Models.EmployeeModel>

ViewData[“Title”] = “Index”;

<table class=”table”>
@Html.DisplayNameFor(model => model.UserName)
@foreach (var item in Model)
@Html.DisplayFor(modelItem => item.UserName)

Using directives will allow the code that is generated from the Razor view to compile correctly. Without using directives, the C# compiler won’t be able to find this Model type. To see the Model type, let us remove the using directive from the Index.cshtml file

ViewImport Error


You will see one of the errors that states that the type or namespace EmployeeModel could not be found(are you missing a using directive or an assembly reference?). It might be because several of your views need that same using directive. So, instead of placing that inside each view, let us create a View import in the Views folder. This will add using statements to every view with just a right-click on the Views folder and selecting Add → New Item.

Dot net core mvc viewImports

By Adding ViewImport.cshtml file under view named “Home” and adding three namespace mentioned above screenshot, the project is build successfully. it means ViewImport.cshtml contains comman namespace which are required in each view. so we don’t need to add same name space to each view.

Now, all the views that appear in this folder or any subfolder will be able to use types from DotNetCoreRepository.Models without specifying that exact using statement. Let us run your application again and you can see that the view is working now.

The settings that are specified in the _ViewImports file present in the shared folder will overwrite by the settings specified in the _ViewImports file in the home folder, So if we use different controller and add viewImport.cshtml file separately to each controller’s view  then viewimport.cshtml file under shared folder are overridden by each separately used ViewImports files.

Summary :

In this tutorial we have learn ViewImports in ASP.NET Core MVC with Example, By using ViewImport.cshtml file we don’t need to add same namespace to each view rather then we can add it to seperate file called ViewImport.cshtml.

Also Learn More Tutorial :


Leave a Reply

Your email address will not be published.