简体   繁体   中英

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

Have you tried getting the controller from the filterContext

var controller = filterContext.Controller as BaseController;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM