In this article we are going to discuss How controller finds a view. Before starting this article please go through the previous article of Views in ASP.NET Core MVC. If you want to learn ASP .Net Core MVC from beginner to advanced level please follow ASP .Net Core Series.

What is View Discovery ?

  • When we return a View from action method then View Discovery takes place.
  • The View Discovery determines which view file will be used for the action method.
ASPNet Core structure

How application finds a View ?

By default ASP .Net Core MVC application will search View in the below location.

  • Views/Controller/ViewName.cshtml
  • Views/Shared/ViewName.cshtml

Return View from Action Method

  • We can use View() method to return a view file from action method.
  • If the name of View is equal to name of Action method then we return as
     return View() ;                                            
  • If the name of View is equal to name of Action method then we return as
     return View(“ViewName”) ;

Return View from Action Method with Model

  • Model is a property class or entities that can hold the information and can be display in View .
  • If the name of View is equal to name of Action method then we return as
    return View(objectmodel) ;
  • If the name of View is not equal to name of Action method then we return as
    return View("ViewName", objectmodel ) ;

Return View from Other Location from the Project

  • Absolute path
  • Relative path

Who is responsible for View Discovery ?

Razor View Engine.

  • Razor View engine is a markup syntax which helps us to write HTML and server-side code in web pages using C# or VB.NET. It is server-side markup language however it is not at all a programming language.
  • Razor is a template engine and ASP.NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not have any ties with ASP.NET MVC.

Summary

So far in this article we discuss about Views discovery in ASP.NET Core application. If have any suggestion for improvement of this article then please feel to write to us.

Pin It on Pinterest