Make the controller available to filters + Sample

GitHub WebFX #300
This commit is contained in:
Yishai Galatzer 2014-04-28 16:30:20 -07:00
parent 297bb5d36d
commit bcd0974823
5 changed files with 29 additions and 8 deletions

View File

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

View File

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

View File

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

View File

@ -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<string, object> routeValues, ActionDescriptor actionDescriptor)
@ -31,5 +32,10 @@ namespace Microsoft.AspNet.Mvc
public ModelStateDictionary ModelState { get; private set; }
public ActionDescriptor ActionDescriptor { get; private set; }
/// <summary>
/// The controller is available only after the controller factory runs.
/// </summary>
public object Controller { get; set; }
}
}

View File

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