Welcome Guest, you are in: Login

Castle Project

RSS RSS

Navigation (MonoRail)





Search the wiki
»

PoweredBy

Creating a filter

RSS
Modified on 2010/05/10 08:47 by Martijn van Rheenen Categorized as Filters
To create a filter, create a class that implements the IFilter interface, then associate the filter with your controller.

Image
Filtered controllers

You can always create an abstract controller class and associate a filter with it and make your controllers extend it.




using Castle.MonoRail.Framework;

public class AuthenticationFilter : IFilter
{
	public bool Perform(ExecuteEnum exec, IRailsEngineContext context, Controller controller)
	{
		if (context.Session.Contains("user"))
		{
			return true;
		}
		else
		{
			context.Response.Redirect("account", "login");
		}

		return false;
	}
}

The Perform return value indicates to the framework if the process should be ended. If you return false no further process will happen for the current request. It is important that you take some action before, like in the example above, issuing a redirect.

The ExecuteEnum parameter informs the filter about the context of the invocation. It is also used on the FilterAttribute to define when you want to have the filter executed. The possible values are listed in the table below:

ExecuteEnum fieldsDescription
BeforeActionThe filter is invoked before the action.
AfterActionThe filter is invoked after the action.
AfterRenderingThe filter is invoked after the rendering.
AlwaysThe filter is invoked around all steps.

To associate the filter with the controller, use the FilterAttribute:


using Castle.MonoRail.Framework;

[FilterAttribute(ExecuteEnum.BeforeAction, typeof(AuthenticationFilter))]
public class AdminController : Controller
{
	public void Index()
	{
	}
}

ScrewTurn Wiki version 3.0.4.560. Some of the icons created by FamFamFam.