ASP.NET MVC: A General Overview (Part 2)
Published on October 7, 2010 | Filed Under MVCWelcome to part 2 of our introduction to MVC! We left off yesterday with an explanation about how the MVC system is broken up.
So to get a little more into detail, let’s explore MVC’s key function: Routing.
What is Routing?
It’s important to mention that your file structure will completely be changed, should you decide to upgrade. Everything is neatly tucked away in your three folders: Models, Views and Controllers. However, as far as the Views are concerned, the location on your server’s hard drive is not the same as what the URL points to. MVC must “route” your request to an associated folder. In essence, it is taking a request meant for one folder and sending it to another folder.
I think this can best be shown with an example. Assume that you are creating a new MVC 2 website. The following could potentially be your initial file structure:
- Controllers
- HomeController.cs
- Models
- Views
- Home
- Index.aspx
- Home
- Global.asax
- Web.config
This is a very strange structure, but it works! If you notice, your Index.aspx file is three folders down, but this is the default page for your domain. This is because your Global.asax file is responsible for routing requests to that folder. IIS doesn’t know that page exists and, quite frankly, it really doesn’t care. MVC will receive the request, find your file and return it, as though there’s no issues. This method is known as URL Rewriting.
Controllers Cause Association
Just because you drop in a folder named Home with a file named Index.aspx doesn’t mean it’s going to work. MVC would have no clue where to route your request. This is where your controllers, along with the routing settings in your Global.asax file, come into play.
This is very important: Nobody told me originally, but a controller is only associated with a folder, because you name the controller {Folder Name}Controller.cs. It really has no other way to know about it. This is why our folder is named Home and our controller is named HomeController. Make sense?
What’s in the Controller?
To make this site work, this is the entire code for HomeController.cs:
using System; using System.Web; using System.Web.Mvc; namespace NewProject.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } } }
The Breakdown
All controllers must inherit from the System.Web.Mvc.Controllers class.
public class HomeController : Controller
All functions in the controller return a System.Web.Mvc.ActionResult object.
public ActionResult Index()
Your ActionResult must be named what the requested page coming in will be named. For example, Index.aspx must have a corresponding Index ActionResult function. If you didn’t want your Index.aspx page to be referenced as Index there are ways to accomplish this, but I’ll save that for a more advanced overview.
All functions must return a View object.
return View();
The View object is just MVC’s code for “the associated page”.
There’s a lot of information here, but it’s very important that you understand what a controller is and why it’s so important. If you have any questions or concerns, please don’t hesitate to leave a comment in the comment box.
Come back tomorrow for the third and final chapter, where I’ll give an overview of what the Views are.
~Derek Torrence