tags:

views:

79

answers:

1

I have a base controller class from which my other controllers are inherited

public abstract class BaseController : Controller
{
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);
        ...
    }
}

During initialization I'm doing some setup, and there are a few cases where I'd want to short circuit the execution, jumping directly to the return of the ActionResult, skipping the execution of the actual Action method entirely. Something along these lines

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    if(specialCase)
    {
     ViewData[...] = specialCaseInformation;
     return View("~/Shared/SpecialCase.aspx");
    } 
}

The intention would be to skip whatever ActionResult method was going to be called and replace it with my global special case page. But I don't think Initialize was meant for this.

What I think I need to do is create a seperate ActionFilterAttribute class, override the OnActionExecuting method, and if the specialCase comes up, construct a ViewResult object and assign it to the filterContext.Result property.

Am I going in the right direction with this, or should I be doing this differently?

A: 

Yes, an ActionFilterAttribute is exactly the right way. Look at HandleErrorAttribute.cs for an example.

Initialize is not the right way, as you say.

Craig Stuntz