Retrieving a list of action filters from the base controller

In short: Does anyone know the path from the base controller to get a list of actions? Filters applied to the current execution action?

Long: I am using the ASP.NET MVC 1.0 framework. I have an actionMilter RequireSSL that I recently created to check if someone leaves the check and returns to the repository, I would like to forward them back to the unsafe version of the site.

This would be useful in the base controller (I use a custom base controller that inherits from the default controller) to find out which actionFilters are applied to the current action.

I could include this in global.asax.cs. I think any recommendations here will be appreciated.

thank

+3
source share
2 answers

You can create an ActionFilter and implement OnActionExecuting. From this attribute you can 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
+3
source

Ok, here is what I ran using.

 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);
    }
}

Then I added the action attribute to the generated SuperController.

[HandleConnectionSecurity]
Public class SuperController: Controller

+1
source

Source: https://habr.com/ru/post/1734118/


All Articles