Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why call base.OnActionExecuting(filterContext);?

I am just looking at some old code of mine and I have an action filter(OnActionExecuting method)

and at the end of it I have

 base.OnActionExecuting(filterContext); 

Why searching around I see this quite a few times. I also see that in my old Authorize tag I call the base up.

Should I be always be calling the base methods after?

like image 397
chobo2 Avatar asked Jun 19 '11 00:06

chobo2


People also ask

How do you call OnActionExecuting?

OnActionExecuting: It is called just before the action method is going to call. OnActionExecuted: It is called just after the action method is called. OnResultExecuting: It is called just before the result is executed; it means before rendering the view.

What is filter context in MVC?

We need situations where you want to execute some logic before or after an action method executes. ASP.NET MVC provides filters for this. ASP.NET MVC filter is a custom class where you can write logic that needs to be executed before or after an action is called.


2 Answers

Should I be always be calling the base methods after?

That will depend on the situation.

For example in authorization filters (deriving from AuthorizeAttribute) if you call the base method then all the existing authorization logic built into ASP.NET MVC will be executed. If you don't call it, only your authorization logic will be applied.

As far as other standard action filters are concerned (deriving from ActionFilterAttribute) all the OnActionExecuting, OnActionExecuted, OnResultExecuting and OnResultExecuted are defined as virtual but their body is empty, so it doesn't make any difference if you call or not the base method.

like image 116
Darin Dimitrov Avatar answered Oct 02 '22 09:10

Darin Dimitrov


I believe if you let Visual Studio automatically produce an overriding method by typing override and selecting a method in intellisense and pressing tab, Visual Studio will automatically add base.MethodName() in the method body.

If you don't need it, remove it. I've always removed it when creating my ActionFilters and haven't had a problem yet. The only time you shouldn't remove it is when you need the base method to be called.

like image 22
xTRUMANx Avatar answered Oct 02 '22 10:10

xTRUMANx