From bcd0974823283bafa1a40d4beaa70bf3a26da9e3 Mon Sep 17 00:00:00 2001 From: Yishai Galatzer Date: Mon, 28 Apr 2014 16:30:20 -0700 Subject: [PATCH] Make the controller available to filters + Sample GitHub WebFX #300 --- .../Filters/AgeEnhancerFilterAttribute.cs | 10 +++++++++- samples/MvcSample.Web/FiltersController.cs | 13 +++++++++---- samples/MvcSample.Web/Models/User.cs | 2 ++ src/Microsoft.AspNet.Mvc.Core/ActionContext.cs | 6 ++++++ .../ReflectedActionInvoker.cs | 6 +++--- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs b/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs index dc251bbd03..123ea29b2c 100644 --- a/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs +++ b/samples/MvcSample.Web/Filters/AgeEnhancerFilterAttribute.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNet.Mvc; +using System; +using Microsoft.AspNet.Mvc; namespace MvcSample.Web.Filters { @@ -8,6 +9,13 @@ namespace MvcSample.Web.Filters { object age = null; + var controller = context.Controller as FiltersController; + + if (controller != null) + { + controller.User.Log += "Age Enhanced!" + Environment.NewLine; + } + if (context.ActionArguments.TryGetValue("age", out age)) { if (age is int) diff --git a/samples/MvcSample.Web/FiltersController.cs b/samples/MvcSample.Web/FiltersController.cs index 748ff9e59d..c40b06b3ff 100644 --- a/samples/MvcSample.Web/FiltersController.cs +++ b/samples/MvcSample.Web/FiltersController.cs @@ -14,7 +14,12 @@ namespace MvcSample.Web [UserNameProvider(Order = -1)] public class FiltersController : Controller { - private readonly User _user = new User() { Name = "User Name", Address = "Home Address" }; + public User User { get; set; } + + public FiltersController() + { + User = new User() { Name = "User Name", Address = "Home Address" }; + } // TODO: Add a real filter here [ServiceFilter(typeof(PassThroughAttribute))] @@ -25,12 +30,12 @@ namespace MvcSample.Web { if (!string.IsNullOrEmpty(userName)) { - _user.Name = userName; + User.Name = userName; } - _user.Age = age; + User.Age = age; - return View("MyView", _user); + return View("MyView", User); } public ActionResult Blocked(int age, string userName) diff --git a/samples/MvcSample.Web/Models/User.cs b/samples/MvcSample.Web/Models/User.cs index 10fbf112ad..a92366ede6 100644 --- a/samples/MvcSample.Web/Models/User.cs +++ b/samples/MvcSample.Web/Models/User.cs @@ -13,5 +13,7 @@ namespace MvcSample.Web.Models public User Dependent { get; set; } public bool Alive { get; set; } public string Password { get; set; } + + public string Log { get; set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionContext.cs b/src/Microsoft.AspNet.Mvc.Core/ActionContext.cs index a626b5b875..c85e86a56c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionContext.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionContext.cs @@ -11,6 +11,7 @@ namespace Microsoft.AspNet.Mvc : this(actionContext.HttpContext, actionContext.Router, actionContext.RouteValues, actionContext.ActionDescriptor) { ModelState = actionContext.ModelState; + Controller = actionContext.Controller; } public ActionContext(HttpContext httpContext, IRouter router, IDictionary routeValues, ActionDescriptor actionDescriptor) @@ -31,5 +32,10 @@ namespace Microsoft.AspNet.Mvc public ModelStateDictionary ModelState { get; private set; } public ActionDescriptor ActionDescriptor { get; private set; } + + /// + /// The controller is available only after the controller factory runs. + /// + public object Controller { get; set; } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/ReflectedActionInvoker.cs b/src/Microsoft.AspNet.Mvc.Core/ReflectedActionInvoker.cs index 6f0a168925..46ea97851b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ReflectedActionInvoker.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ReflectedActionInvoker.cs @@ -60,6 +60,8 @@ namespace Microsoft.AspNet.Mvc _filters = GetFilters(); _cursor = new FilterCursor(_filters); + _actionContext.Controller = _controllerFactory.CreateController(_actionContext); + // >> ExceptionFilters >> AuthorizationFilters >> ActionFilters >> Action await InvokeActionExceptionFilters(); @@ -360,12 +362,10 @@ namespace Microsoft.AspNet.Mvc { _cursor.SetStage(FilterStage.ActionMethod); - var controller = _controllerFactory.CreateController(_actionContext); - var actionMethodInfo = _descriptor.MethodInfo; var actionReturnValue = await ReflectedActionExecutor.ExecuteAsync( actionMethodInfo, - controller, + _actionContext.Controller, _actionExecutingContext.ActionArguments); var underlyingReturnType = TypeHelper.GetTaskInnerTypeOrNull(actionMethodInfo.ReturnType) ?? actionMethodInfo.ReturnType;