Reputation: 1637
The code below was copied from this post. I see in other posts the same thing, such as in the answer for this this post.
Why is the line base.OnActionExecuting(filterContext);
included inside the overall OnActionExecuting
methods?
What purpose does it fulfill?
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// do some irrelevant stuff
base.OnActionExecuting(filterContext);
}
public ActionResult Index()
{
return View();
}
}
Upvotes: 1
Views: 732
Reputation: 5459
Short answer:
currently, the line base.OnActionExecuting(filterContext)
does not do anything (performs no work). The functional behavior will be the same with or without the line.
Longer answer:
The .NET source code for Controller.OnActionExecuting
looks as follows (taken from here):
protected virtual void OnActionExecuting(ActionExecutingContext filterContext)
{
}
If you look at the OnActionExecuting
method, you'll see this method is completely empty. This is the code/method that base.OnActionExecuting(filterContext)
is calling. You don't have to call base.OnActionExecuting(filterContext)
as it currently does nothing however I would recommend doing so. The reason behind this is that if .NET framework is updated such that the Controller.OnActionExecuting
method actually has needed code it in it, then the needed code will not be called without including the line in question. This could lead to some scenarios that can be pretty difficult and time-consuming to debug. It's also certainly possible for new framework code to be introduced that does break your code when you call base.OnActionExecuting
however this scenario is generally easier to spot and fix than the other. Of course in the end, it's all going to come down to what code the framework introduces and what it breaks for how easy it is to identify and resolve.
As a side-note, .NET framework is in maintenance mode now so I highly doubt the example I mentioned above with .NET framework updating the code in Controller.OnActionExecuting
will ever happen however it serves as a good best practice in other similar scenarios as well.
Upvotes: 1