Conventions Used In MVC to Find View

In this article we will understand How Conventions Used In MVC to Find View By Sagar Jaybhay.









See the above image in this we are creating Index methods below is code for that.





public ActionResult Index(string EmpID)

Employee employee = new BusinessLogic
.Business().GetEmployee(EmpID);

return View(employee);





In the above code, we don’t use view names we simply use view and pass employee object to that view. So the question is how asp.net MVC knows which view is call and display?





Which location asp.net MVC used to find view. For understanding this we create one method in our EmployeeController whose name is wheretofindview and simply return View is used but we never created a view for that by doing this we will understand from which location view is called.









See the first line by convention it is finding wheretofindview as view name and location which is found





  1. First, it is searching in views folder in that controller specific folder in our case it is EmployeeController. So it will try to find a view in the Employee folder.
  2. If in controllers folder it not found then it will go for Shared folder inside the Views folder.




Means Location where to find the view is





  1. Views / Controller name folder
  2. Views / Shared folder




As Per View Extension





  1. Aspx
  2. Ascx:- partial view
  3. Cshtml :- razor view
  4. Vbhtml:- razor view








If your View name is different from the action name then you need to specify the full path in View to get that view as given below.





 return View("~/Views/Employee/wheretofindview.cshtml");




Summary





  1. MVC is Convention over configuration
  2. View folder contains a folder for every controller in our application along with a shared folder
  3. A shared folder is used to share views that are used for more than one controller. Mainly master page and layout page are stored in a shared folder.








GitHub :- https://github.com/Sagar-Jaybhay/MVC5

Comments

Popular posts from this blog

How to Start a YouTube Channel Free - Complete Guide

How Angular know which module is the startup module?

How to Start a YouTube Channel Free - Complete Guide