Table of contents
- 1 Name convention
- 2 Areas
- 3 Actions
- 4 Redirecting
- 5 Useful Properties
- 5.1 Request/Response
- 5.2 Controller information
- 5.3 Others
- 6 Default Action
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.
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:
| Name | Description |
|---|---|
| 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
| Property | Type | Description |
|---|---|---|
| Context | Castle.MonoRail.Framework.IRailsEngineContext | Gets the context of this request execution. |
| Session | IDictionary | Gets the Session dictionary. |
| Flash | Castle.MonoRail.Framework.Flash | Gets a dictionary of volative items. Ideal for showing success and failures messages. |
| HttpContext | System.Web.HttpContext | Gets the web context of ASP.NET API. |
| Request | Castle.MonoRail.Framework.IRequest | Gets the request object. |
| Response | Castle.MonoRail.Framework.IResponse | Gets the response object. |
| Params | NameValueCollection | Shortcut to IRequest.Params |
| Form | NameValueCollection | Shortcut to IRequest.Form |
| Query | NameValueCollection | Shortcut to IRequest.QueryString |
| IsClientConnected | bool | Shortcut to IResponse.IsClientConnected |
Controller information
| Property | Type | Description |
|---|---|---|
| Name | string | Gets the controller's name (as MonoRail knows it) |
| AreaName | string | Gets the controller's area name. |
| LayoutName | string | Gets or set the layout being used. |
| Action | string | Gets the name of the action being processed. |
| SelectedViewName | string | Gets 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
| Property | Type | Description |
|---|---|---|
| Logger | Castle.Core.Logging.ILogger | Logger for the controller (you must enable logging first) |
| IsPostBack | bool | Determines 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"); } }