Oleg Sh
Oleg Sh

Reputation: 9013

WebAPI : Get controller object from System.Web.Http.Filters.ActionFilterAttribute

I want to call some controller's method from System.Web.Http.Filters.ActionFilterAttribute (pay attention, it's WebApi, not MVC).

I found ControllerDescriptor object, but found only way to create new controller instance, not using current:

var controllerDescriptor = actionContext.ActionDescriptor.ControllerDescriptor;

var controller = (BaseApiController)controllerDescriptor.CreateController(actionContext.Request);
var companyId = controller.GetCompanyIdFromClaims();

is it possible to get current instance of controller?

Upvotes: 2

Views: 873

Answers (1)

Nkosi
Nkosi

Reputation: 246998

You get it from the action context's controller context, which has a

public IHttpController Controller { get; set; }

property

HttpControllerContext.Controller Property

For example:

var controller = (MyControllerType)actionContext.ControllerContext.Controller;

Upvotes: 2

Related Questions