exam questions

Exam 70-486 All Questions

View all questions & answers for the 70-486 exam

Exam 70-486 topic 1 question 70 discussion

Actual exam question from Microsoft's 70-486
Question #: 70
Topic #: 1
[All 70-486 Questions]

HOTSPOT -
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:

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 drop-down list in the answer area.)
Hot Area:

Show Suggested Answer Hide Answer
Suggested Answer:
Target 1: IActionFilter -
MVC3 introduced a completely new pattern to configure filters for controllers and its actions. While injection of filter attributes is still supported itis recommended using this new pattern for filter configuration because it has the advantage to support constructor injection and does not require the InjectAttribute anymore.
First of all you have to create your filter class by implementing one of the filter interfaces e.g. IActionFilter.
Target 2: public void OnActionExecuting(ActionExecutingContext filterContext)
Target 3: public void OnActionExecuted(ActionExecutedContext filterContext)
Reference: Dependency injection for filters
https://github.com/ninject/Ninject.Web.Mvc/wiki/Dependency-injection-for-filters

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Burgyn
Highly Voted 5 years, 3 months ago
ActionFilterAttribute, because "override" keyword can't be used with interface ;-)
upvoted 23 times
rhysabray
5 years, 2 months ago
Good spot!
upvoted 2 times
...
...
drizet
Highly Voted 5 years, 3 months ago
Target 1 is ActionFilterAttribute, because it implements IResultFilter Target 2 is OnActionExecuting because log should be written before Action Executed Target 3 is OnResultExecuted, because log should be written after result is returned
upvoted 23 times
majco333
5 years, 3 months ago
https://exceptionnotfound.net/asp-net-mvc-demystified-action-filters/
upvoted 2 times
...
desona
4 years, 11 months ago
agreed
upvoted 1 times
...
...
pwhsoft
Most Recent 4 years, 3 months ago
This is the Interface namespace Microsoft.AspNetCore.Mvc.Filters { /// <summary> /// A filter that surrounds execution of the action. /// </summary> public interface IActionFilter : IFilterMetadata { /// <summary> /// Called before the action executes, after model binding is complete. /// </summary> /// <param name="context">The <see cref="ActionExecutingContext"/>.</param> void OnActionExecuting(ActionExecutingContext context); /// <summary> /// Called after the action executes, before the action result. /// </summary> /// <param name="context">The <see cref="ActionExecutedContext"/>.</param> void OnActionExecuted(ActionExecutedContext context); } }
upvoted 3 times
...
nelaed
4 years, 9 months ago
Right answer is Target 1: Option 2) public class LogActionFilter : ActionFilterAttribute Target 2: Option 3) OnActionExecuting(ActionExecutingContext filterContext) Target 3: Option 6) OnResultExecuted(ResultExecutedContext filterContext) https://stackoverflow.com/questions/32035121/custom-action-filter-in-mvc
upvoted 6 times
elevator44
4 years, 6 months ago
This is correct. 1) You must first implement Interface method declarations and then you can override them in sub classes derived from class where implementation is made and marked as virtual. 2) https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.onactionexecuting?view=aspnet-mvc-5.2 3) https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.onresultexecuted?view=aspnet-mvc-5.2
upvoted 1 times
throne
4 years, 5 months ago
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-custom-action-filters This link says : Inherit the CustomActionFilter class from ActionFilterAttribute and then make CustomActionFilter class implement IActionFilter interface. So, looks like answer doesn't have correct options to choose from.
upvoted 1 times
...
...
fbelhocine
4 years, 3 months ago
Target 1 cannot be IActionFilter and the class method's with override
upvoted 1 times
...
...
hungry_ape9000
5 years ago
FINAL ANSWER: public class LogActionFilter : ActionFilterAttribute OnActionExecuting(ActionExecutingContext filterContext) OnActionExecuted(ActionExecutedContext filterContext)
upvoted 2 times
mr_
4 years, 11 months ago
I have a feeling that the final answer may be the following (I am nearly sure about first two, not sure about the third one): public class LogActionFilter : ActionFilterAttribute OnActionExecuting(ActionExecutingContext filterContext) OnResultExecuted(ActionExecutedContext filterContext)
upvoted 2 times
mr_
4 years, 11 months ago
"After results are returned, information about the results also must be written to the log." It could mean that for example after view is returned to the browser. That is why I thought about OnResultExecuted. The execution order of these events is as follows: 1. OnActionExecuting 2. OnActionExecuted 3. OnResultExecuting 4. OnResultExecuted
upvoted 2 times
nelaed
4 years, 9 months ago
You are right. https://stackoverflow.com/questions/32035121/custom-action-filter-in-mvc
upvoted 1 times
...
...
...
...
hungry_ape9000
5 years ago
FINAL ANSWER: ActionFilterAttribute OnActionExecuting OnActionExecuted
upvoted 5 times
...
tanujgyan
5 years ago
I think onActionExecuted should be the right answer
upvoted 2 times
...
aousos
5 years ago
it s actionFilterAttribute not IFilter because we don't have override when we implement interfaces
upvoted 3 times
...
cristopherpereiramoreira
5 years, 8 months ago
You can but ActionFilter is better folllowing SoC and extensibility points
upvoted 2 times
...
Aghie
5 years, 8 months ago
I think so too as you cant implement OnResulting and OnResulted without implementing IResultFilter
upvoted 1 times
...
Jimi_K
5 years, 8 months ago
The image shown in the solution shows the incorrect answer highlighted for Target 3. However the text below does detail the correct solution (OnActionExecuted...)
upvoted 7 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago