From 6170ac1924a683d26d35d72d2dc502a82ca781bd Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 16 Jul 2015 11:10:45 -0700 Subject: [PATCH] Add an event notification for when the action is invoked --- .../MvcRouteHandler.cs | 7 +++++++ .../MvcRouteHandlerTests.cs | 18 +++++++++++++++++ .../Notification/TestNotificationListener.cs | 20 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs b/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs index d9ac3b3d2a..b761f1a59a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs +++ b/src/Microsoft.AspNet.Mvc.Core/MvcRouteHandler.cs @@ -85,6 +85,13 @@ namespace Microsoft.AspNet.Mvc await InvokeActionAsync(context, actionDescriptor); context.IsHandled = true; } + + if (_notifier.ShouldNotify("Microsoft.AspNet.Mvc.ActionInvoked")) + { + _notifier.Notify( + "Microsoft.AspNet.Mvc.ActionInvoked", + new { actionDescriptor, httpContext = context.HttpContext }); + } } finally { diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs index 2140f03309..56703aafda 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/MvcRouteHandlerTests.cs @@ -179,6 +179,24 @@ namespace Microsoft.AspNet.Mvc Assert.Contains(routeValues, kvp => kvp.Key == "tag" && string.Equals(kvp.Value, "value")); } + [Fact] + public async Task RouteAsync_Notifies_ActionInvoked() + { + // Arrange + var listener = new TestNotificationListener(); + + var context = CreateRouteContext(notificationListener: listener); + + var handler = new MvcRouteHandler(); + + // Act + await handler.RouteAsync(context); + + // Assert + Assert.NotNull(listener?.ActionInvoked.ActionDescriptor); + Assert.NotNull(listener?.ActionInvoked.HttpContext); + } + private RouteContext CreateRouteContext( ActionDescriptor actionDescriptor = null, IActionSelector actionSelector = null, diff --git a/test/Microsoft.AspNet.Mvc.TestCommon/Notification/TestNotificationListener.cs b/test/Microsoft.AspNet.Mvc.TestCommon/Notification/TestNotificationListener.cs index eaf9013029..e40dd22cc3 100644 --- a/test/Microsoft.AspNet.Mvc.TestCommon/Notification/TestNotificationListener.cs +++ b/test/Microsoft.AspNet.Mvc.TestCommon/Notification/TestNotificationListener.cs @@ -23,11 +23,31 @@ namespace Microsoft.AspNet.Mvc.TestCommon.Notification }; } + public OnActionInvokedEventData ActionInvoked { get; set; } + + [NotificationName("Microsoft.AspNet.Mvc.ActionInvoked")] + public virtual void OnActionInvoked( + IHttpContext httpContext, + IActionDescriptor actionDescriptor) + { + ActionInvoked = new OnActionInvokedEventData() + { + ActionDescriptor = actionDescriptor, + HttpContext = httpContext, + }; + } + public class OnActionSelectedEventData { public IActionDescriptor ActionDescriptor { get; set; } public IHttpContext HttpContext { get; set; } public IRouteData RouteData { get; set; } } + + public class OnActionInvokedEventData + { + public IActionDescriptor ActionDescriptor { get; set; } + public IHttpContext HttpContext { get; set; } + } } }