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 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.
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:
| 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. 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"); } }