Views in MVC – Session 17

In this session of ASP.NET Core Tutorial, we describe Views in MVC. In simple word, View is a file with cshtml extension (cs is instance of CSharp programming language) that includes html syntax and embedded Razor code. Views are normally located inside Views folder and they are responsible to present Model Data that procced by Controller.

What is Views in MVC?

View is a file with cshtml extension (cs is instance of CSharp programming language) that includes html syntax and embedded Razor code. Views are normally located inside Views folder and they are responsible to present Model Data that procced by Controller.

Default Views Route in ASP.NET Core

You can return View as a result of an Action Method just when the Action Method result is ViewResult or IActionResult. If you look at the definition of ViewResult you can realized that ViewResult is implemented from ActionResult and IActionResult. Thus, you can set result of Action Method as ViewResult to return View or even IActionResult if you want to return View or wider range of result same as JSON Result.

If your Action Method return a View as a result, by default, ASP.NET Core is looking for a file inside Views folder which is located in root of your project, then a sub-folder that its name should be equal Controller name, and finally a file that its name should be equal Action Method name with cshtml (if your programming language is C#) extension.

If ASP.NET Core will not be able to find above mentioned file, then it is looking for the file inside Views/Shared folder and finally inside Pages/Shared folder. For instance, as you can see in below example, ASP.NET Core is looking for index.cshtml file inside below mention path in the order priority.

  • Views/Home/Index.cshtml
  • Views/Shared/Index.cshtml
  • Pages/Shared/Index.cshtml
Default ASP.NET Core MVC Routing for Views

You can change this default routing that we will talk about it during the next session.

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

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments