Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current controller instance in another class?

In my project I manage exception in a class like this

 public class PortalBusinessExceptionManager
    :IBusinessExceptionManager
{
    public void HandleBusinessException(BusinessExceptionDto exceptionDto)
    {
        //TODO
    }
}

And I have a BaseController in my project that all controllers inherit from it

public class BaseController : Controller
{
    public void SetNotification(string message, ExceptionType type)
    {
       //TO DO
    }
}

So I want that in HandleBusinessException method how can I get an instance from current controller and invoke SetNotification for it

 public void HandleBusinessException(BusinessExceptionDto exceptionDto)
{
    var controller=....???
     controller.SetNotification(); 
}
like image 805
M.Azad Avatar asked Jul 09 '26 02:07

M.Azad


2 Answers

From the answer from this question you can create your own ControllerFactory and put the controller instance in the session for later retrieval.

public class MyControllerFactory : DefaultControllerFactory
{
    public override IController CreateController(RequestContext requestContext, string controllerName)
    {
        var controller = base.CreateController(requestContext, controllerName);
        HttpContext.Current.Session["controllerInstance"] = controller;
        return controller;
    }
}

Then you register it in your Apllication_Start:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

And you can than call the instance in your exception handling method.

public void HandleBusinessException(BusinessExceptionDto exceptionDto)
{
     var controller= HttpContext.Current.Session["controllerInstance"] as BaseController;
     if(controller != null) 
     {
         controller.SetNotification(); 
     }
}

Note that this is a pretty hacky solution (session may expire for example and your method wont be called), you should probably be better off overriding OnException method in your BaseController and call SetNotification there

public class BaseController : Controller
{
    public void SetNotification(string message, ExceptionType type)
    {
       //TO DO
    }

    protected override void OnException(ExceptionContext filterContext)
    {
        SetNotification(filterContext.Exception.Message, yourExceptionType);
        base.OnException(filterContext);
    }
}
like image 110
Davor Zlotrg Avatar answered Jul 11 '26 17:07

Davor Zlotrg


Not sure if it solves your issue yet it can be helpful:

// get current controller instance
var controller = DependencyResolver.Current.GetService<MyController>();
// pass current request context
controller.ControllerContext = new ControllerContext(Request.RequestContext, controller); 
// call method
controller.MethodName();
like image 35
Paweł Avatar answered Jul 11 '26 15:07

Paweł



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!