Reputation: 11
You are developing an ASP.NET MVC application. Before an action is executed, information about the action must be written to a log. After results are returned, information about the results also must be written to the log. You need to log the actions and results. You have the following code:
**Target 1**
{
public override void **Target 2**
{
Logger.Log("ActionLog", filterContext.RouteData);
}
public override void **Target 3**
{
Logger.Log("ResultLog", filterContext.RouteData);
}
}
Which code segments should you include in Target 1, Target 2 and Target 3 to implement the LogActionFilter class? (To answer, select the appropriate option from the list)
Option 1) public class LogActionFilter : IActionFilter
Option 2) public class LogActionFilter : ActionFilterAttribute
Option 3) OnActionExecuting(ActionExecutingContext filterContext)
Option 4) OnActionExecuted(ActionExecutedContext filterContext)
Option 5) OnResultExecuting(ResultExecutingContext filterContext)
Option 6) OnResultExecuted(ResultExecutedContext filterContext)
From blog and other sources, I found 2 type of answers. Some chose Option 2, Option 3 and Option 5 respectively. If this is the case, then my question is why not Option 6 instead of Option 5.
Please help me. I appreciate your valuable response.
Upvotes: 0
Views: 1385
Reputation: 1503
Relating to this link
Target 1: Option 2) public class LogActionFilter : ActionFilterAttribute
Because ActionFilterAttribute is the base class for all the attribute filters.This class implements both the IActionFilter and IResultFilter interfaces and inherits from the Filter class. It provides the following methods to execute a specific logic after and before controller action's execution:
1- OnActionExecuting(ActionExecutedContext filterContext): Just before the action method is called.
2- OnActionExecuted(ActionExecutingContext filterContext): After the action method is called and before the result is executed (before view render).
3- OnResultExecuting(ResultExecutingContext filterContext): Just before the result is executed (before view render).
4- OnResultExecuted(ResultExecutedContext filterContext): After the result is executed (after the view is rendered).
So,accordingly from the description of the methods above:
Target 2: Option 3) OnActionExecuting(ActionExecutingContext filterContext)
Target 3: Option 6) OnResultExecuted(ResultExecutedContext filterContext)
Upvotes: 2