neuquen
neuquen

Reputation: 4169

Call controller method from action filter

I have my base controller and action filter in the same namespace but in different classes. I created a class inside of the base controller which requests http headers, and I would like to call that method inside of my action filter.

If I do a simple Details dtls = GetHeaders() the intelliSense asks if I want to create another method GetHeaders() inside of the action filter.

So my question is can I call the GetHeaders() method inside of the BaseController class directly from the action filter? How would I do so? If not, how could I call that method?

namespace Infrastructure
{
    public class BaseController
    {
        public Details GetHeaders()
        {
            //Get the headers
        }
    }

    public class MyFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            //Call GetHeaders() to get Header1 data
        }
    }
}

Upvotes: 1

Views: 6382

Answers (2)

Fals
Fals

Reputation: 6839

public class MyFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        filterContext.HttpContext.Response.Redirect("~/BaseController/GetHeaders");
    }
}

Upvotes: 0

Colin Bacon
Colin Bacon

Reputation: 15609

Have you tried getting the controller from the filterContext

var controller = filterContext.Controller as BaseController;

controller.GetHeaders();

Upvotes: 7

Related Questions