Neil M.
Neil M.

Reputation: 456

How do I invoke OnActionExecuting when I call another controller's ActionResult function

I have 2 MVC projects. There is a common page that I want to reference directly instead of having to re-write. I'm doing so like this:

public ActionResult test()
{
    OldController.Controllers.PatronController temp = new OldController.Controllers.PatronController();

    return temp.Index();
}

The problem is, in "OldController" there is some functionality that get set in the OnActionExecuting function. When I call the function like I'm doing above, OnActionExecuting isn't being called.

How can I share the common page between the two projects (so I can return it OnActionExecuting?

Upvotes: 1

Views: 711

Answers (2)

Ameen
Ameen

Reputation: 2586

Create a third project that is a class library and inside that library create a ControllerBase class that has the common functionality between the two controllers and in each project derive from that controller class.

Upvotes: 1

Dave Alperovich
Dave Alperovich

Reputation: 32500

Ideally, you want to keep navigation consistent in each project and still re-use the core functionality.

For that reason, I recommend:

1) creating a 3rd MVC Project with an Action Partial (Partial with Action Method -- and Controller).

2) Add a reference for your new MVC project to your 2 primary MVC projects.

3) In each of your primary MVC projects, create Views that render your Action Partial within a View.

Upvotes: 2

Related Questions