ASP.NET MVC: Order of view selection
Few days ago, I and one of my friends were discussing basic of ASP.NET MVC. After some discussion we came on point in which order views are selected. Because there can be more than one view with same name. For e.g. we can have index.aspx, index.ascx, index.cshtml, index.vbhtml and all this can be in controller specific folder as well shared folder. So in which order view is getting selected? Which view has higher priority? So to getting it clearly, quickly I changed action method to return view which does not exists as mentioned below.
public ActionResult Index()
{
return View("NotExists");
}
We built application and run it and it throw an exception The view 'NotExists' or its master was not found or no view engine supports the searched locations. The following locations were searched:
"A picture is worth a thousand words" Based on above exception and image we can conclude following order of view selection.
- View/[CONTROLLER]/[VIEW].aspx
- View/[CONTROLLER]/[VIEW].ascx
- View/Shared/[VIEW].aspx
- View/Shared/[VIEW].ascx
- View/[CONTROLLER]/[VIEW].cshtml
- View/[CONTROLLER]/[VIEW].vbhtml
- View/Shared/[VIEW].cshtml
- View/Shared/[VIEW].vbhtml
So hope this quick post would be helpful to understand order of view selection in ASP.NET MVC. Stay tuned to upcoming post to know how we can change this order of view selection and how it could hit performance gain.
You can follow me on twitter for latest link and update on ASP.NET & MVC.