How can I use RedirectToAction in the base controller class?

Now I am switching to a web application on the MVC 2 website. I have a base controller that inherits all my other controllers. I have some business logic that I put in an overridden Execute method that will redirect the user to an action if necessary.

Each controller action should trigger the same logic, and I would not want to send calls to my function in every action method. I wanted to center the code. On the old website, this logic was executed in the main page load event, but the transition of this logic to MVC 2 was difficult.

+3
source share
3 answers

You need to call the method ExecuteResultfor the returned one ActionResult.

In addition, you should override ExecuteCore, not Execute. Otherwise you will lose ControllerContext.

By the way, you should consider writing an ActionFilter and loading it dynamically .

+2
source

Depending on when the code should run, you can override the OnActionExecuting and / or OnActionExecuted methods in your base controller class.

0
source

. ( )

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    //You function here
}

. - , .

0
source

Source: https://habr.com/ru/post/1779322/


All Articles