Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Managing Shared Directives and Layouts in ASP.NET Core MVC Views

Tech May 13 3

Utilizing _ViewImports.cshtml for Shared Directives

Common directives used across multiple views can be centralized in a _ViewImports.cshtml file. This file supports the following Razor directives:

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

It does not support other Razor features like function or section definitions.

Example _ViewImports.cshtml file:

@using MyMvcApp
@using MyMvcApp.DataModels
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper "MyMvcApp.CustomTagHelpers.AlertTagHelper, MyMvcApp"

In a typical ASP.NET Core MVC project, the _ViewImports.cshtml file is placed in the Views directory. However, it can be located in any subfolder. When placed in a subfolder, its directives apply to views with in that folder and its child folders.

Directives are processed hierarchically, starting from the root Views folder and proceeding through each subfolder until reaching the view's location. Settings specified at a root level can be overridden at a folder level.

When multiple _ViewImports.cshtml files affect a single view, their directives combine with specific precedence rules:

  • @addTagHelper, @removeTagHelper: All directives are executed in order.
  • @tagHelperPrefix: The directive closest to the view takes precedence.
  • @using: All using statements are aggregated.
  • @inherits: The directive closest to the view overrides others.
  • @inject: For each property, the instance closest to the view overrides any others with the same property name.
  • @model: The directive closest to the view overrides others.

Executing Code Before Views with _ViewStart.cshtml

Code that must run before each full view (excluding layouts and partials) can be placed in a _ViewStart.cshtml file, typically located in the Views folder. Like _ViewImports.cshtml, this file follows a hierarchical structure. If a _ViewStart.cshtml is defined in a controller-specific subfolder, it runs after the one defined in the root Views folder.

Example _ViewStart.cshtml:

@{
    Layout = "_MainLayout";
}

This code configures all views to use the _MainLayout.cshtml layout file by default.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.