3

I want to call a controller method from a class and get the controller context without making requests to the controller. What are possible ways of doing that?

I can call method by creating an object of a controller class but I am unable to get controller's context.

var controllerObj = new HomeController() controllerObj.methodA(); 

and in methodA request context is not available.

1 Answer 1

4

Get the instance of a Controller in a class using DependencyResolver.

public class Example { public static void CallActionMethod() { var controller = DependencyResolver.Current.GetService<AboutController>(); controller.ControllerContext = new ControllerContext(System.Web.HttpContext.Current .Request.RequestContext, controller); controller.Index(); } } 

Reference

Sign up to request clarification or add additional context in comments.

1 Comment

I already have done like that,but as your answer is correct so I am marking it correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.