tags:

views:

46

answers:

2

In Short: Does any know a way from the base controller to get a list of actionFilters being applied to the current executing action?

The Long: I am using ASP.NET MVC 1.0 framework. I have a "RequireSSL" actionFilter that I've recreated for checking out, however, if someone leaves the checkout and goes back to the store I would like to forward them back to non-secure version of the site.

It would be helpful in the base controller (I am using a custom base controller that inherits from the default Controller) to find out what actionFilters are being applied to the current action.

I could include this into the global.asax.cs I guess, any guidance here would be appreciated.

Thanks

+1  A: 

You can create an ActionFilter and implement OnActionExecuting. From this Attribute you could redirect them.

public sealed class MyRedirectAttributeAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        if (!filterContext.ActionDescriptor.IsDefined(typeof(RequireSSLAttribute), true))
        {
            filterContext.HttpContext.Response.Redirect("~/Controller/Action");
        }

        base.OnActionExecuting(filterContext);
    }
}true
Thad
Wouldn't I have to add this filter to every single action? The current setup, I have an actionFilter called "RequireSSL", which works great with forwarding the browser to SSL if that action has this filter, but for all non-ssl actions I don't want to require another filter to be applied.. Essentially making it so that every single action requires either a non-ssl or ssl required filter would be a bad way to go I think..
ETHODE Web Development
This is placed on the BaseController not each action. It test running action.We have done something similar. The RedirectAttribute does the branching based on what attributes it finds on the action. The Attribute on the action is not an ActionFilter.
Thad
I did add this onto the initialize method in my base controller, but it doesn't appear to be running.. Which I find odd.. Is there another location I should put it?
ETHODE Web Development
add it to the on the BaseController class not a method.
Thad
+1  A: 

Well, here is what I wound up with..

 public sealed class HandleConnectionSecurityAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpRequestBase req = filterContext.HttpContext.Request;
        HttpResponseBase res = filterContext.HttpContext.Response;

        if (!filterContext.ActionDescriptor.IsDefined(typeof(RequiresSSL), true) && HttpContext.Current.Request.IsSecureConnection)
        {
            var builder = new UriBuilder(req.Url)
            {
                Scheme = Uri.UriSchemeHttp,
                Port = 80
            };
            res.Redirect(builder.Uri.ToString());
        }

        base.OnActionExecuting(filterContext);
    }
}

I then added an action attribute to the SuperController I created.

[HandleConnectionSecurity]
public class SuperController : Controller

ETHODE Web Development