Home

Castle Stronghold

Table of contents

Controller basics

Controllers are extremelly important as they are the intelligent piece of the application that orchestrates the application flow. The Getting started section depicted the basic usage of controllers.

By default, the class name is used as the controller identification. If your controller's class name ends with Controller, the controller identifier is the class name, but stipped of Controller. You can also use the ControllerDetails attribute to associate a different name to your controller class.

Name convention

It is advisable that controller classes follow the Name + Controller suffix convention. When the controller is registered, MonoRail strips the "Controller" suffix and uses the name as the key. The controller name is used on the URL to access it.

The attribute ControllerDetailsAttribute can be used to define the exact name for the controller. For example:


using Castle.MonoRail.Framework;

[ControllerDetails("cust")]
public class CustomerController : Controller
{

}

Instead of Customer, the ControllerDetailsAttribute has defined cust as the name that will be used in URLs to identify this controller.

Areas

MonoRail supports the concept of areas, which are logical groups of controllers. All controllers belong to an area. The default area is an empty (unnamed) one.

Imagine a tree of controllers. Each branch in the tree is an area, each leaf is a controller.

To define an area, use the ControllerDetailsAttribute attribute:


using Castle.MonoRail.Framework;

[ControllerDetails(Area="admin")]
public class UsersController : Controller
{
    public void Index()
    {
    }
}

This controller is now is accessible using the following url path:

/admin/users/index.rails

You can also define more than one level:


using Castle.MonoRail.Framework;

[ControllerDetails(Area="admin/users")]
public class PasswordMngController : Controller
{
    public void Index()
    {
    }
}

This controller now is accessible using the following url path:

/admin/users/passwordmng/index.rails

Actions

We define actions as the procedures that can be invoked on your controller. Basically it translates to any public instance method your controller exposes.

Quick Note

If you do not want a specific method to be "invocable", it must be non-public.

Redirecting

The Controller offers a handful of Redirect overloads. Some of them allow you to pass along query string parameters.

The following table list some of the overloads:

NameDescription
RedirectToAction(string action)Redirects to another action in the same controller.
RedirectToAction(String action, params String[] queryStringParameters)Redirects to another action in the same controller specifying query string entries.
RedirectToAction(String action, IDictionary parameters)Redirects to another action in the same controller specifying query string entries.
Redirect(String url) Redirects to the specified URL.
Redirect(String url, IDictionary parameters)Redirects to the specified URL specifying query string entries.
Redirect(String controller, String action)Redirects to another controller and action.
Redirect(String area, String controller, String action)Redirects to another controller and action (within an area).

Useful Properties

The following properties exposed by the Controller are very likely to be used all the time while developing an application.

Request/Response

PropertyTypeDescription
ContextCastle.MonoRail.Framework.IRailsEngineContextGets the context of this request execution.
SessionIDictionaryGets the Session dictionary.
FlashCastle.MonoRail.Framework.FlashGets a dictionary of volative items. Ideal for showing success and failures messages.
HttpContextSystem.Web.HttpContextGets the web context of ASP.NET API.
RequestCastle.MonoRail.Framework.IRequestGets the request object.
ResponseCastle.MonoRail.Framework.IResponseGets the response object.
ParamsNameValueCollectionShortcut to IRequest.Params
FormNameValueCollectionShortcut to IRequest.Form
QueryNameValueCollectionShortcut to IRequest.QueryString
IsClientConnectedboolShortcut to IResponse.IsClientConnected

Controller information

PropertyTypeDescription
NamestringGets the controller's name (as MonoRail knows it)
AreaNamestring Gets the controller's area name.
LayoutNamestringGets or set the layout being used.
ActionstringGets the name of the action being processed.
SelectedViewNamestringGets or sets the view which will be rendered by this action. We encourage you to use RenderView or RenderSharedView instead of setting this property.

Others

PropertyTypeDescription
LoggerCastle.Core.Logging.ILoggerLogger for the controller (you must enable logging first)
IsPostBackboolDetermines if the current Action resulted from an ASP.NET PostBack. As a result, this property is only relavent when using WebForms views. It is placed on the base Controller for convenience only to avoid the need to extend the Controller or provide additional helper classes.

Default Action

The DefaultActionAttribute attribute provides a way to associate a default action method that will be called if a matching action method can not be found on the controller. One possible use is so that a web designer can add views without the need for a developer to add new action methods to the class. To associate a default action with your controller, use the DefaultActionAttribute attribute. This attribute can only be applied at the class level.

If you don't specify a name in the DefaultActionAttribute, the method DefaultAction will be used.


[DefaultAction]
public class HomeController : Controller
{
    public void Index()
    {
    }

    public void DefaultAction()
    {
    // default to a "not found" template
        string template = "notfound";

    // check and see if a template exists for the 
    // action indicated in the URL
        if (HasTemplate("home/" + Action))
        {
            template = Action;
        }

        RenderView(template);
    }
}

In the following example, the code specifies the name of the action to be invoked


[DefaultAction("Foo")]
public class HomeController : Controller
{
    public void Index()
    {
    }

    public void Foo()
    {
        RenderText(Action + " was not found");
    }
}
Google
Search WWW Search castleproject.org