View Import In ASP.NET CORE
In this tutorial we will discuss View Import in ASP.NET Core MVC.
The ASP.NET Core MVC and Razor comes with a lot of new advanced features for working with the Razor views. ViewImports is one of the new features and will discuss usage and scenario.
In ASP.NET Core MVC Application, the _ViewImports.cshtml file provides a way to include the directives globally for Razor Pages so that we don’t have to add them individually in each and every view. the _ViewImports.cshtml file supports the following directives:
- @addTagHelper
- @tagHelperPrefix
- @removeTagHelper
- @namespace
- @inject
- @model
- @using
The @addTagHelper, @tagHelperPrefix, and @removeTagHelper directives are basically used to manage Tag Helpers. The @namespace directive is basically used to specify the namespace that the ViewImports belongs to. With the help of @inject directive, it supports Dependency injection. We already use the @model directive in our previous applications when we are working with models.
The @model directive is basically used to specify the Model for your view. The @using directive basically used to include the common namespaces globally so that you don’t have to include the namespaces in each and every view page.
Lets understand viewimports with an example if you are new to this video series and understand viewimport example let’s create a model called Students within the Models folder of your application which we have already created while implementation of model so you refer our previous video on models in asp.net core.
public class Student { /// /// GTechFluent Student Information ///
public int Id { get; set; } public string Name { get; set; } public string Class { get; set; } public int Age { get; set; } public string School { get; set; } }
As of now we have two action methods. One action method is used to display all the student data while the other action method takes the student id as a parameter and returns that student information details.
public ViewResult Details(int id) { StudentViewModel studentViewModel = new StudentViewModel(); studentViewModel.Student = _studentRespository.GetStudent(id); studentViewModel.StudentAddress = _studentRespository.GetStudentAddress(id); return View(studentViewModel); } public ViewResult StudentList() { List lstStudent = _studentRespository.GetAllStudent(); return View(lstStudent); }
If we go to details view and list view where where have used the StudentModel with @model detectives and how we are accessing this our @model SampleProjectCore.StudentViewModel and our project is common for application so we need to declare that global and not require to use in every view.
In MVC, _ViewImports.cshtml files are created within the Views or within the subfolder of the Views folder. To create the “_ViewImports.cshtml” file right-click on the Views folder and then select the “Add – New Item” option from the context menu, which will open the “New Item” window. From the “New Item” window search for “Razor” and then select the “Razor View Import” and click on the “Add” button as shown in the below image which should create the “_ViewImport.cshtml” within the “Views” folder.
Once the _ViewImports.cshtml file is created, the add namespace into viewimport we do need to use a fully qualified name each time.
@using SampleProjectCore
Just like the _ViewStart file, the _ViewImports file is also hierarchical. It is also possible to pace the _ViewImports in the subfolder of the Views folder as shown in the below image. Here we have one _ViewImports file in the Views folder and another _ViewImports file within the Home folder. So if you want some import specific to Home you need to add into this file.
You can watch our video version of this tutorial with step by step explanation.