2

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?

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.