As you know, the Visual Studio template creates some standard views with instructions to start with personalization. In this post, I will show you how to analyze the controller for the default URL and the view associated with it.
The Home Controller
If you open the /Controllers/HomeController.cs file you will find the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MyTasks.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "xxxxxxxxxxxx"; return View(); } ... some similar methods for the other views } } |
Here are the things that you need to note:
Class name
The name of the controller is “Home”. The class that define this controller must complete is name with the “Controller” prefix.
Output type of every action is “ActionResult”
Your action must display some HTML code, so the return type could be a simple string. By specifying “ActionResult”, .Net will authomatically add some extra code that set additional response headers, such as the content type sent to the browser. To better understand this, try to add the following method to the controller:
1 2 3 |
public string Test(){ return "<html><body><p>Hello, world!</p></body></html>"; } |
If you call the URL /Home/Test you will see an HTML page like Index, but if you analyze the page information with your browser you will notice that Content-type is not set.
When an action render an HTML page it will be better to use the class ViewResult, derived from ActionResult, but ActionResult is more generic and allows to manage cases where Action lead, for example, to a redirection.
ViewBag.Message
The ViewBag is an object that can be used to share data between controller and view.
return View()
Is a simple instruction that do all the work. By default this method call the view, in the folder /Views/<controller> with the same name of the action: for us is /Views/Home/Index.cshtml. It’s time to open it to understand the additional operations done.
A Base View
Opening the view file you will find something that, for a PHP programmer, is very familiar! This strictly resemble a PHP file with a mix of HTML and programming code instruction.
At a first approach the only difference you will note is that code blocks are surrounded by the following syntax:
1 2 3 |
@{ ... C# code } |
The call to View() function in the controller performs the following actions:
- loads the specified view or the default one
- uses the view engine specified in the Web.config file to parse the view file and execute the code in it
- sets response parameters
- outputs the HTML (or other) code to the browser
There are 2 things to note in the Index view code:
The first one is the way how the message set in the controller is displayed. You will find in the code the following instruction:
1 |
<h2>@ViewBag.Message</h2> |
It’s something like the PHP code:
1 |
<h2><?=ViewBag->Message?></h2> |
The ViewBag object, as stated before, is created in the controller and can be accessed by the view.
The second thing to note is that the page is not complete. In the page you will find only the “content” section of the page and the parts that can be considered as a template, such as the header, the menu and the footer, are not present.
HostForLIFE.eu as The Best and Recommended ASP.NET MVC 5.0 Hosting Provider
Get high performance, best uptime and the most reliable for your ASP.NET MVC 5.0 Hosting from HostForLIFE.eu. HostForLIFE.eu provides a safe, reliable and performance-driven foundation for your website. If you are looking for the right ASP.NET MVC 5.0 hosting provider, they are the right choice for you. They have proactive monitoring down to seconds with reactive solutions in place to ensure the stability of the services they provide. All hosting servers are monitored 24/7/365. They use enterprise software to monitor their entire network infrastructure. Their best and recommended ASP.NET MVC 5.0 hosting price starts from Є3.00/month, It includes with Unlimited bandwidth, Unlimited disk space, and complete features. To that end, they have built an industry leading Windows and PHP web hosting platform featuring the best of modern technology and industry practices.