So far we have looked at the requirements for our fictional application and the data structure that goes with that, the application’s Model using a repository pattern , and URL Routing to provide friendlier URLs.

What we have covered so far really is just supporting code. In this post we’ll look the first of two parts that really hold our application together, the Controller. In the next post we’ll cover the View and how it ties into the Controller.

 

What Are Controllers?

Controllers are the glue that hold the MVC pattern together. Every request made to our application will get mapped to a Controller. This Controller will handle any work we need to do like gathering data from the Model and rendering Views. For example if our application receives a request of http://yourdomain.com/Task/Show/2 ASP.Net MVC would map the request to, using the default mapping, a controller called TaskController. That Controller might look like this:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Mvc;
   6: namespace Dev102MvcApp.Controllers
   7: {
   8:     public class TaskController : Controller
   9:     {
  10:         public ActionResult Show()
  11:         {
  12:             throw new NotImplementedException();
  13:         }
  14:     }
  15: }

As you can see, a Controller is just a class that inherits from the Controller base class. The inheritance brings some very handy features that we’ll look at as this post progresses.

 

What Are Controller Actions?

Controller Actions are methods of your controller class that get called based on the request your application receives. For example, using the controller we defined above, if our application received a request like http://yourdomain.com/Task/Show/45 then the Show() method of our TaskController would get called because Show is the action portion of our application’s route table. You can read more about URL Routing in the previous post. From a security stand point it is important to know that just about any method of a controller that is public can potentially get called just by providing the correct action name in the URL. It isn’t hard to imagine how this could get abused by someone with less than honorable intention. It is important to only expose controller methods if they actions that you intend your application to use. Everything else should be kept private. Controller actions cannot be overloaded and they cannot be made static.

 

What Are ActionResults?

Controller actions must return a result. These results must inherit from the ActionResult class. There are 6 action result types in ASP.Net MVC. ViewResult A ViewResult will return HTML code. This is the most commonly used result type. EmptyResult An EmptyResult returns nothing. RedirectResult A RedirectResult will redirect to another URL. RedirectToRouteResult A RedirectToRouteResult will redirect to another controller action. JsonResult A JsonResult will return a result in JavaScript Object Notation. This can be used in an AJAX request. ContentResult A ContentResult returns text. If an action returns a type other than one inherited from ActionResult, it will automatically convert the object to a string and wrap the result in a ContentResult. For example the following example would return a ContentResult with a value of “45.75″.

   1: public class TaskController : Controller
   2: {
   3:     public double HoursSpent()
   4:     {
   5:         return 45.75;
   6:     }
   7: }

 

 

Final Thoughts

That pretty much wraps up this post. You should have a good understanding of Controllers and Actions now and how to use them in your applications. In the next post will wrap up this series by looking at the View which is the final piece of the MVC puzzle. To make sure you don’t miss a beat grab the Dev102 RSS feed. You may also be interested to follow what I have to say on my web development blog or grab my RSS feed.

Tags :

Post a Comment