Hello you all lovely people out there, presenting you guys with the 5th article in the MVC Series.
In this one, we will see as to how a particular View is selected to be rendered onto the browser, what exactly is a View, what are View Engines and stuffs.
Let's deep dive in.
View is a HTML page which is visible to the user, user sees it, does operations on the View, which then might generate a different View or else.
Views are HTML files with extension as .cshtml/.vbhtml, depending upon the programming language used to code (C#/VB.NET).
If we have a look at the Views directory in the MVC project, we will see there are various folders over there, as below:
ViewBag is a mechanism via which we can transfer data from a controller to a view, we will study about it in detail later in the article.
Lets try to run the project and have a look at our newly created view, click on the Play Icon, the app will start under IIS Express, a random port will be assigned to the app, and we will see the view in the browser as below:
We see the route as "/Home/PlayFootball". The runtime goes to the "Home" directory in the Views folder, looks for "PlayFootball.cshtml", finds it and renders it onto the browser.
Let us now change the name of our view to "PlayFootball123.cshtml", and see what happens, how does the MVC engine react.
Let us browse to the earlier url as "http://localhost:16861/Home/PlayFootball", we get a yellow screen stating the below error:
Server Error in '/' Application.
The view 'PlayFootball' or its master was not found or no view engine supports the searched locations. The following locations were searched:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The view 'PlayFootball' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/PlayFootball.aspx
~/Views/Home/PlayFootball.ascx
~/Views/Shared/PlayFootball.aspx
~/Views/Shared/PlayFootball.ascx
~/Views/Home/PlayFootball.cshtml
~/Views/Home/PlayFootball.vbhtml
~/Views/Shared/PlayFootball.cshtml
~/Views/Shared/PlayFootball.vbhtml
We are trying to browse to the "PlayFootball" view, but it doesn't exist at the moment, the MVC engine tries to find it in the Home folder first, it doesn't find it in Home, then it goes to the Shared folder, doesn't find it over there either. In the end, it provides us with the error that the view "PlayFootball" doesn't exist.
We see in the above error that the MVC engine searched for .aspx and .ascx files as well.
~/Views/Home/PlayFootball.aspx
In this one, we will see as to how a particular View is selected to be rendered onto the browser, what exactly is a View, what are View Engines and stuffs.
Let's deep dive in.
View is a HTML page which is visible to the user, user sees it, does operations on the View, which then might generate a different View or else.
Views are HTML files with extension as .cshtml/.vbhtml, depending upon the programming language used to code (C#/VB.NET).
If we have a look at the Views directory in the MVC project, we will see there are various folders over there, as below:
Well, the folder names are in accordance with the Controller names, i.e. if we have 2 controllers with names as "HomeController" and "AccountController", we will have 2 folders in the Views directory with the names as "Home" and "Account".
In the above screenshot, we can see that we have one controller "HomeController", hence one folder in the Views directory with the name as "Home".
Also, we can see that we have a folder named as "Shared" in the Views directory, what does it do? Well, as the name implies, this "Shared" folder provides some shared views to the project.
We can see that, we have "_Layout.cshtml" file, it basically gives a uniform layout to the all the views present in the project.
"Error.cshtml" is shown on the UI when there's an unhandled error or stuffs.
We will talk about the "Shared" folder and its contents, in a great deal, in the later part of this article.
Let's concentrate on the "Home" views folder, we can see that there are multiple views in the folder, namely "About.cshtml", "Contact.cshtml", "Index.cshtml", where have they come from and why?
Well, individual views files conform to the individual action methods in a particular controller, i.e, we should have action methods as "About", "Contact", "Index" in the "HomeController".
When we look at the HomeController.cs file, we can see the below code which proves that there's a direct proportion between action methods and views.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
Let's see the below code in detail:
public ActionResult Index()
{
return View();
}
We are returning a View() and the return type is ActionResult, let's see as to what's the exact relation and how does the C# compiler understand the relationship between View() and ActionResult.
If we select the View(), and press F12 from the keyboard, we will see that we are taken to the definition, which says,
// Summary:
// Creates a System.Web.Mvc.ViewResult object that renders a view to the response.
//
// Returns:
// The System.Web.Mvc.Controller.View result that renders a view to the response.
protected internal ViewResult View();
Let's again press F12 on ViewResult type, we will see that the definition is as below:
// Summary:
// Represents a class that is used to render a view by using an System.Web.Mvc.IView
// instance that is returned by an System.Web.Mvc.IViewEngine object.
public class ViewResult : ViewResultBase
Let's again press F12 on ViewResultBase type, the definition which will come out will be as follows:
// Summary:
// Represents a base class that is used to provide the model to the view and then
// render the view to the response.
public abstract class ViewResultBase : ActionResult
We see that ViewResultBase type inherits from ActionResult, so ultimately View() inherits from ActionResult, and hence the code
public ActionResult Index()
{
return View();
}
compiles and works quite successfully.
A pictorial representation for more simplicity is given as below:
Let us try to add a new Action method and a new View, let's see what happens:
Go to the HomeController, and add the below code:
public ActionResult PlayFootball()
{
ViewBag.Message = "Euro 2016 coming up. Stay Tuned !!!";
return View();
}
Right click on the action method, and click "Add View", a pop-up will be opened by VS as below:
We will see that the View name textbox is pre-populated with the action method name as "PlayFootball", thus a direct relation between action method and view name.
Let us try to retrieve the message from the action method in the view via the ViewBag dynamic object.
Lets try to run the project and have a look at our newly created view, click on the Play Icon, the app will start under IIS Express, a random port will be assigned to the app, and we will see the view in the browser as below:
We see the route as "/Home/PlayFootball". The runtime goes to the "Home" directory in the Views folder, looks for "PlayFootball.cshtml", finds it and renders it onto the browser.
Let us now change the name of our view to "PlayFootball123.cshtml", and see what happens, how does the MVC engine react.
Let us browse to the earlier url as "http://localhost:16861/Home/PlayFootball", we get a yellow screen stating the below error:
Server Error in '/' Application.
The view 'PlayFootball' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/PlayFootball.aspx
~/Views/Home/PlayFootball.ascx
~/Views/Shared/PlayFootball.aspx
~/Views/Shared/PlayFootball.ascx
~/Views/Home/PlayFootball.cshtml
~/Views/Home/PlayFootball.vbhtml
~/Views/Shared/PlayFootball.cshtml
~/Views/Shared/PlayFootball.vbhtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: The view 'PlayFootball' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/PlayFootball.aspx
~/Views/Home/PlayFootball.ascx
~/Views/Shared/PlayFootball.aspx
~/Views/Shared/PlayFootball.ascx
~/Views/Home/PlayFootball.cshtml
~/Views/Home/PlayFootball.vbhtml
~/Views/Shared/PlayFootball.cshtml
~/Views/Shared/PlayFootball.vbhtml
We are trying to browse to the "PlayFootball" view, but it doesn't exist at the moment, the MVC engine tries to find it in the Home folder first, it doesn't find it in Home, then it goes to the Shared folder, doesn't find it over there either. In the end, it provides us with the error that the view "PlayFootball" doesn't exist.
We see in the above error that the MVC engine searched for .aspx and .ascx files as well.
~/Views/Home/PlayFootball.aspx
~/Views/Home/PlayFootball.ascx
Where have these extensions come from?
Well MVC supports 2 view engines by default, the ASPX view engine which was the default one in MVC 1 and MVC 2, and the Razor view engine which was launched with MVC3.
Views with .aspx and .ascx used to be formed with the ASPX view engine, these views are given priority over the Razor views, i.e the .cshtml ones, hence the MVC engine searches for the .aspx views as well.
Till MVC 4 and VS 2012, we could have ASPX and Razor Views working simultaneously, but with VS 2013, ASPX views did not fit well as per the StackOverflow post (http://stackoverflow.com/questions/19696842/using-aspx-view-engine-with-mvc-5).
I am still to explore ASPX views in VS 2015. If I get proper stuffs, I will write an article about it too.
This is it for this article guys, will be looking at Razor views, Layouts, Partial views, and the various mechanisms to pass in data from the controller to the view, in the next article.
Criticisms are most welcome. Till then see ya and stay tuned.


