From 28fee3470fc24c84fd2ea68e60b8cc318a0caf41 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 5 May 2014 13:45:17 -0700 Subject: [PATCH] Controller base class now implements IActionFilter --- samples/MvcSample.Web/FiltersController.cs | 5 +++ samples/MvcSample.Web/SimplePocoController.cs | 34 ++++++++++++++++++- src/Microsoft.AspNet.Mvc.Core/Controller.cs | 22 +++++++++++- .../Filters/DefaultFilterProvider.cs | 11 ++---- .../Microsoft.AspNet.Mvc.Core.kproj | 1 - 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/samples/MvcSample.Web/FiltersController.cs b/samples/MvcSample.Web/FiltersController.cs index d824167527..71505e69e6 100644 --- a/samples/MvcSample.Web/FiltersController.cs +++ b/samples/MvcSample.Web/FiltersController.cs @@ -78,5 +78,10 @@ namespace MvcSample.Web { throw new Exception(message); } + + public override void OnActionExecuting(ActionExecutingContext context) + { + ViewBag.DidTheFilterRun = "Totally!"; + } } } \ No newline at end of file diff --git a/samples/MvcSample.Web/SimplePocoController.cs b/samples/MvcSample.Web/SimplePocoController.cs index f5a4ec3563..fa5ac51e03 100644 --- a/samples/MvcSample.Web/SimplePocoController.cs +++ b/samples/MvcSample.Web/SimplePocoController.cs @@ -15,13 +15,45 @@ // See the Apache 2 License for the specific language governing // permissions and limitations under the License. +using System.Diagnostics; +using System.Globalization; +using Microsoft.AspNet.Mvc; + namespace MvcSample.Web { - public class SimplePocoController + public class SimplePocoController : IActionFilter, IResultFilter { + private Stopwatch _timer; + public string Index() { return "Hello world"; } + + public void OnActionExecuting(ActionExecutingContext context) + { + _timer = Stopwatch.StartNew(); + } + + public void OnActionExecuted(ActionExecutedContext context) + { + var time = _timer.ElapsedMilliseconds; + context.HttpContext.Response.Headers.Add( + "ActionElapsedTime", + new string[] { time.ToString(CultureInfo.InvariantCulture) + " ms" }); + } + + public void OnResultExecuting(ResultExecutingContext context) + { + _timer = Stopwatch.StartNew(); + } + + public void OnResultExecuted(ResultExecutedContext context) + { + var time = _timer.ElapsedMilliseconds; + context.HttpContext.Response.Headers.Add( + "ResultElapsedTime", + new string[] { time.ToString(CultureInfo.InvariantCulture) + " ms" }); + } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Controller.cs b/src/Microsoft.AspNet.Mvc.Core/Controller.cs index a3e85cf5b8..1992be1f49 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controller.cs @@ -18,6 +18,7 @@ using System; using System.Security.Principal; using System.Text; +using System.Threading.Tasks; using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.ModelBinding; @@ -25,7 +26,7 @@ using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc { - public class Controller + public class Controller : IActionFilter, IAsyncActionFilter { private DynamicViewData _viewBag; @@ -223,5 +224,24 @@ namespace Microsoft.AspNet.Mvc { return new RedirectToRouteResult(Url, routeName, routeValues, permanent: true); } + + public virtual void OnActionExecuting([NotNull] ActionExecutingContext context) + { + } + + public virtual void OnActionExecuted([NotNull] ActionExecutedContext context) + { + } + + public virtual async Task OnActionExecutionAsync( + [NotNull] ActionExecutingContext context, + [NotNull] ActionExecutionDelegate next) + { + OnActionExecuting(context); + if (context.Result == null) + { + OnActionExecuted(await next()); + } + } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/DefaultFilterProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/DefaultFilterProvider.cs index e9b8e886eb..62eee1473c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/DefaultFilterProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/DefaultFilterProvider.cs @@ -88,14 +88,6 @@ namespace Microsoft.AspNet.Mvc.Filters ApplyFilterToContainer(filterItem.Filter, filterFactory); } - - var controllerFilter = context.ActionContext.Controller as IFilter; - if (controllerFilter != null) - { - // If the controller implements a filter, we want it to be the first to run. - var descriptor = new FilterDescriptor(controllerFilter, FilterScope.Action); - context.Result.Insert(0, new FilterItem(descriptor, controllerFilter)); - } } private void InsertControllerAsFilter(FilterProviderContext context, IFilter controllerFilter) @@ -104,8 +96,9 @@ namespace Microsoft.AspNet.Mvc.Filters // run closest to the action. int order = Int32.MaxValue; var orderedControllerFilter = controllerFilter as IOrderedFilter; - if (orderedControllerFilter == null) + if (orderedControllerFilter != null) { + order = orderedControllerFilter.Order; } diff --git a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj index 8ce7dd1ad8..c99bf0c57a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj +++ b/src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj @@ -110,7 +110,6 @@ -