Customize ASP.NET Core View Discovery – Session 18

Customize View Discovery ASP.NET Core - Session 18

In this session of ASP.NET Core Tutorial we talk about customization of ASP.NET Core View Discovery. In Session 17 we described Views and also ASP.NET Core default view discovery. Having said that, this session is continuation of pervious session, thus we highly recommend watching previous session before starting this part of our tutorial.

How we can change default ASP.NET Core View Discovery?

  • Step1: Select proper View() overload
  • Step2: Create your View with your custom name what ever you want (View can be created even outside of Views folder)
  • Step3: Pass your created View name and path as an input parameter to View() method.

View() Overloads

View method has four overloads that enable us to modify ASP.NET Core View discovery and pass model data to View which will be discussed during up coming sessions.

View Method overload

This overload, doesn’t accept any input parameters and follow ASP.NET Core default View discovery.

This overload version accept a String as an input parameter, so we can change ASP.NET Core default View discovery through this overload.

This overload needs two input parameters for changing default View discovery and passing data form Controller to View which will be discussed during up coming sessions.

This overload just accept an object as input parameter to pass data from Controller to View.

Absolute Path & Relative Path

We can use Absolute and Relative Path to change default View in ASP.NET Core. Absolute path as its name implies, doesn’t need to know about default current path. Absolute path is staring from the root of Web App and all the subfolders should be considered until reaching the target file. Just bear in mind when using Absolute path we need to add View extension (cshtml in case of C# programming language) to the name of View.

But in Relative path, ASP.NET Core consider current path and routing should be completed based on current path. For example, when we call this view Return View("MyView"); in Index Action Method that is in HomeController, ASP.NET Core is looking for MyView View, inside Views/Home path. Also, We don’t need to add View File extension when we use Relative Path.

For going a directory up in Relative Path we can use ../

Absoulte Path & Relative Path

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 and 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


0 0 votes
Article Rating

Inline Feedbacks
View all comments