Sometimes we need to call a function every time we get an instance of a controller (someone say constructor?).
However, the constructor has a bad limitation about the operations it can do. The HttpContext object isn’t initialized.
Let’s take a scenario. We had a cookie that needs be readed and initialized every time.
A way is to read it in every Action we have in the Controller, another is to override the OnActionExecuting method.
Here an example:
public class SiteBaseController : Controller
public override void OnActionExecuting(ActionExecutingContext context)
var lang = Request.Cookies.FirstOrDefault(x => x.Key == "lang");
In this case, the overwritten method is in a Base Class that will be extended by any controller (is a middleware class between the asp .net core Controller class and the MVC controller class). In this case my cookie will be readed in every request.
However you can simple overwrite the method in a single controller if you need that.