Add an event notification for when the action is invoked

This commit is contained in:
Ryan Nowak 2015-07-16 11:10:45 -07:00
parent 8e402af47e
commit 6170ac1924
3 changed files with 45 additions and 0 deletions

View File

@ -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
{

View File

@ -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,

View File

@ -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; }
}
}
}