Conventions Used In MVC to Find View
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
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.
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
Views / Controller name folder
Views / Shared folder
As Per View Extension
Aspx
Ascx:- partial view
Cshtml :- razor view
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
- MVC is Convention over configuration
- View folder contains a folder for every controller in our application along with a shared folder
- 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
Subscribe to my newsletter
Read articles from Sagar Jaybhay directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sagar Jaybhay
Sagar Jaybhay
I am Sr. Software Developer. I am having more than 7 yr's of experience in software field. i am full stack developer. Blogging is my hobby and Trading is my passion.