View Start & View Imports – Session 25

View Start & View Imports - Session 25

In this session of ASP.NET Core tutorial, we discuss about View Start and View Imports and will show you the benefits of using these features. Both options improve your experience of coding specially in Views.

What is View Start?

View Start is a file with type of Views that can set Layout for all Views in a single place. Its extension is same as View file (cshtml C# and vbhtml VB programming language). This file facilitates code maintenance and code duplication. Priority of the View Start files are hierarchical.

View Start

It is a file with extension of cshtml in case of developing application with C# programming language and vbhtml if your development programming language would be VB. The codes inside this file is executed earlier than all View files code. It is recommended creating this file inside Views folder to set Layout file for all Views. but you can create this file even inside all sub-folders of Views folder to set Layout file for Views of that sub-folder.

Moreover, we can still use Layout inside Views (Session 24) which has higher priority. Also, if we set Layout of a View to null, that we will be rendered without any layout file. Priority of the View Start files are hierarchical and its order is as shown below.

  1. Layout inside View file (Session 24)
  2. ViewStart file inside sub-folders
  3. ViewStart file inside Views folder

This file provides us with this opportunity to set Layout file of all Views in one single place. This opportunity reduces duplication code which is against Don’t Repeat Yourself Principle. Also with this feature code, maintenance is facilitated. For instance, imagine a web app with more than hundreds views. If you decide to change its View layout file without this option, You should modify all views that will be time consuming and problematic from error occurrence.

Sample of ViewStart.cshtm

What is View Imports?

View Imports is a file that we can store Views common Namespace and other directive same as AddTagHelper, Inject, Models and etc. This file helps us to avoid entering namespaces and some directives in our Views.

View Imports

It is a file with extension of cshtml in case of developing application with C# programming language and vbhtml if your development programming language would be VB. It is recommended creating this file inside Views folder but you can create this file even inside all sub-folders. Priority of the View Imports files are hierarchical and the file inside sub-folders has higher priority than the file inside root of Views folder.

This file is a place that we can enter Views common namespaces and some directives same as AddTagHelper, Inject, Models and etc. Having said that, namespaces should be added to this file with @using directive.

Sample of ViewImports.cshtml

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube. Also you can have access to list of all sessions HERE.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments