Share via


how to call a controller action from another controller

Question

Monday, January 24, 2011 5:58 AM

hi,

i want to call a controller action from another controller,

and i don't want use Redirect 30x

thank you.

All replies (13)

Monday, January 24, 2011 6:08 AM ✅Answered

Try this. I'm assuming you want to return that action's result.

var ctrl= new MyController();
ctrl.ControllerContext = ControllerContext;
//call action
 return ctrl.Action();

 


Monday, January 24, 2011 9:14 AM ✅Answered

     there are some common methods(action) in /member, and some different methods,    i try to create a controller for deal with common methods

1. Create a plain class( not a controller!) for this and pass arguments- if you have common methods  that does not involves html code

2. Create partial methods / html helpers and call from the view  - if you have html codes


Monday, January 24, 2011 6:25 AM

thank you,but i don't know which action called.

i try this

protected override void HandleUnknownAction(string actionName)
{
    var ctrl = new MyController();

    ctrl.ControllerContext = ControllerContext;

    if (!ctrl.ActionInvoker.InvokeAction(this.ControllerContext, actionName))
    {
        base.HandleUnknownAction(actionName);
    }
}

but does not work


Monday, January 24, 2011 6:52 AM

Could you please say what do you want to achieve? and the MVC version, please


Monday, January 24, 2011 6:52 AM

protected override void HandleUnknownAction(string actionName)
{
    this.ActionInvoker.InvokeAction(

        new System.Web.Mvc.ControllerContext(this.ControllerContext.RequestContext, new MyController()),

        actionName

    );
}

i found when i replace context, it's works. thank you.


Monday, January 24, 2011 7:02 AM

i think it is a bad solution,

one url must do different thing depend on which type of user logon.

they are different thing,  should use different url, but i can't decide.

any mvc version is ok.


Monday, January 24, 2011 7:45 AM

return RedirectToAction("actionname", "controllername");


Monday, January 24, 2011 7:52 AM

return RedirectToAction("actionname", "controllername");

this will Redirect to another url.


Monday, January 24, 2011 8:04 AM

i don't want use Redirect 30x

 

Would you please tell why?


Monday, January 24, 2011 8:08 AM

i want do that like RewritePath

    if use RewritePath, i will check conditions on cookie, and rewrite to different page.


Monday, January 24, 2011 8:22 AM

I would encapsulate the logic you want to call in separate class, then simply instantiate the class and call it. 

Controllers should be a light weight implementation ideally calling functionality that lives in your model or a separate layer, so you should never need to call one action from annother action.  Other than to trigger a redirect (possibly).

For example in virtually every app i have written in the last 10 years i will have separate layers to encapuslate data access, business logic and presentation (standard n-tier stuff), the controller should only access functionality in the bussiness logic layer in general.  By structuring your app like that it allows you to easily share fucntionality between controllers etc, without having to relie on actions in this case performing multiple tasks which could get broken by a subsequent change to your code base.


Monday, January 24, 2011 8:42 AM

there are 2 type of member user,

    when user login, i direct they to

        /member1/index

        or

       /member2/index

    but , i asked combine 2 type to 1,

    there are some common methods(action) in /member, and some different methods,

    i try to create a controller for deal with common methods,

    other methods,  i check user type and call another controller...

   that's why a have this question.

   thank you.


Monday, January 24, 2011 12:27 PM

I concur.  It sounds like you want to create a base class for all users, and then an extended class for the logged-in users that would provide additional operations or services.    One could then pass that information to the view using the same controller.  The information would be different based on the users status.  One might want to consider a decorator pattern.