I know how to get the current controller name
HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
But is there any way to get the current controller instance in some class (not in an action and not in a view)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By default you can only access the current
Controllerinside a controller withControllerContext.Controlleror inside a view withViewContext.Context. To access it from some class you need to implement a customControllerFactorywhich stores the controller instance somewhere and retrieve it from there. E.g in theRequest.Items:Then you register it in your
Application_Start:And you can get the controller instance later:
But I would find some another way to pass the controller instance to my class instead of this “hacky” workaround.