locked
Controller Initialize Method RRS feed

  • Question

  • User-824531797 posted

    In ASP.NET MVC classic, there used to be a controller Initialize method that one could override and put initialization code. Similarly there was OnResultExecuted method where one could put finalization code.

    Above were found super useful for controller base classes. And now not found in ASP.NET Core. Have used OnActionExecuting and Dispose Methods in Asp .Net core controller as new homes for above.

    Q1 - What are the architectural / philosophical reasons behind this removal?

    Further the ControllerBase class (equivalant to ApiController in Asp .Net MVC) does not even have above OnActionExecuting  and Dispose methods. So now could find two remedies - use Controller class as base for ApiControllers as well or use filters.

    Q2 Why exactly ControllerBase doesnt have any methods to put initialization and finalization code ?

    Thanks

    Friday, November 6, 2020 5:42 AM

Answers

  • User-824531797 posted

    Thanks Bruce.

    Based on your answer, did some more investigation by visiting the source code of the Controller class.

    Turns out we can apply IActionFilter and IResultFilter at the Controller Level and get the desired methods as before. As per this document, these are executed before action level filters so would achieve the same effect.

    Controller.cs does that to generate the OnActionExecuting  and OnActionExecuted methods.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 7, 2020 5:57 AM

All replies

  • User-474980206 posted

    Q1:

    the ControllerBase is not equivalent to the old ApiController, the core equivalent is Controller.

    In core there is no ApiController class. You get an api controller by adding the [ApiController] attribute to a class that inherits from Controller:

    Q2:

    ControllerBase does not define any lifecycle events. Those are defined as interfaces and implemented by say the Controller class.

    note: asp.net core was a rewrite of the old asp.net/mvc as a clean slate. Complete backward compatibility was not a goal.

    Friday, November 6, 2020 4:51 PM
  • User-824531797 posted

    Q1: Is actually regarding the MVC Controller (not API controller)


    Q2: That is slightly old information

    pl refer https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1

    Friday, November 6, 2020 4:55 PM
  • User-474980206 posted

    abhisinghal21

    Q2: That is slightly old information

    pl refer https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1

    the webapi template uses ControllerBase because it does not need the lifecycle events. it is still the [ApiController] attribute that makes its an apicontroller. in fact you don't even to need to inherit from ControllerBase.

    a perfectly working api controller:

        [ApiController]
        public class MyClass
        {
            [Route("api/echo/{s}")]
            public string echo (string s)
            {
                return s;
            }
        }

     you just don't get the handy helper methods defined by ControllerBase

    Friday, November 6, 2020 10:40 PM
  • User-824531797 posted

    Thanks Bruce.

    Based on your answer, did some more investigation by visiting the source code of the Controller class.

    Turns out we can apply IActionFilter and IResultFilter at the Controller Level and get the desired methods as before. As per this document, these are executed before action level filters so would achieve the same effect.

    Controller.cs does that to generate the OnActionExecuting  and OnActionExecuted methods.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 7, 2020 5:57 AM