Home

Castle Stronghold

Table of contents

Controller basics

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

The class name is used by default as the controller identification. If your controller's name ends with Controller, it will be stripped from the name. 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 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 force the definition of a name to the controller. For example:


using Castle.MonoRail.Framework;

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

}

The controller defined above will be accessible using cust on the url.

Areas

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

You can think of a tree of controllers. Each node 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 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 refer to action 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 that a specific method be "invocable", it cannot be 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. One possible use is so that a web designer can add views without the need for a developer to add new action methods. To associate a default action with your controller, use the DefaultActionAttribute attribute. This attribute can only be applied at the class level.

Unless you specify which action should be invoked if none is matched, DefaultAction will be used.


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

    public void DefaultAction()
    {
        string template = "notfound";

        if (HasTemplate("home/" + Action))
        {
            template = Action;
        }

        RenderView(template);
    }
}

In the following example, the code specifies 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