diff --git a/samples/MvcSample/Filters/UserNameProvider.cs b/samples/MvcSample/Filters/UserNameProvider.cs new file mode 100644 index 0000000000..303d2814b0 --- /dev/null +++ b/samples/MvcSample/Filters/UserNameProvider.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; + +namespace MvcSample.Filters +{ + public class UserNameProvider : ActionFilterAttribute + { + private static readonly string[] _userNames = new[] { "Jon", "David", "Goliath" }; + private static int _index; + + public override async Task Invoke(ActionFilterContext context, Func next) + { + object originalUserName = null; + + context.ActionParameters.TryGetValue("userName", out originalUserName); + + var userName = originalUserName as string; + + if (string.IsNullOrWhiteSpace(userName)) + { + context.ActionParameters["userName"] = _userNames[(_index++)%3]; + } + + await next(); + } + } +} diff --git a/samples/MvcSample/FiltersController.cs b/samples/MvcSample/FiltersController.cs index f483c22afc..52e3d177f3 100644 --- a/samples/MvcSample/FiltersController.cs +++ b/samples/MvcSample/FiltersController.cs @@ -8,6 +8,8 @@ namespace MvcSample [ServiceFilter(typeof(PassThroughAttribute))] [PassThrough(Order = 0)] [PassThrough(Order = 2)] + [InspectResultPage] + [UserNameProvider(Order = -1)] public class FiltersController : Controller { private readonly User _user = new User() { Name = "User Name", Address = "Home Address" }; @@ -15,9 +17,13 @@ namespace MvcSample // TODO: Add a real filter here [ServiceFilter(typeof(PassThroughAttribute))] [AgeEnhancer] - [InspectResultPage] - public IActionResult Index(int age) + public IActionResult Index(int age, string userName) { + if (!string.IsNullOrEmpty(userName)) + { + _user.Name = userName; + } + _user.Age = age; return View("MyView", _user); diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/ActionResultFilterEndPoint.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/ActionResultFilterEndPoint.cs index 06836cec5a..d83da95d08 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/ActionResultFilterEndPoint.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/ActionResultFilterEndPoint.cs @@ -8,6 +8,12 @@ namespace Microsoft.AspNet.Mvc.Filters { public async Task Invoke(ActionResultFilterContext context, Func next) { + // result can get cleared at any point in the pipeline + if (context.Result == null) + { + context.Result = new EmptyResult(); + } + await context.Result.ExecuteResultAsync(context.ActionContext); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterEndPoint.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterEndPoint.cs index 73f8656480..910cfe0479 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterEndPoint.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/AuthorizationFilterEndPoint.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; namespace Microsoft.AspNet.Mvc.Filters { - // This one lives in the FilterDescriptors namespace, and only intended to be consumed by folks that rewrite the action invoker. + // This one lives in the Filters namespace, and only intended to be consumed by folks that rewrite the action invoker. public class AuthorizationFilterEndPoint : IAuthorizationFilter { public bool EndPointCalled { get; private set; } diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptor.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptor.cs index 6676fb25f4..fab17e9d4e 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptor.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptor.cs @@ -4,13 +4,13 @@ namespace Microsoft.AspNet.Mvc { public class FilterDescriptor { - public FilterDescriptor([NotNull]IFilter filter, int origin) + public FilterDescriptor([NotNull]IFilter filter, int filterScope) { Filter = filter; - Origin = origin; + Scope = filterScope; } public IFilter Filter { get; private set; } - public int Origin { get; private set; } + public int Scope { get; private set; } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptorOrderComparer.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptorOrderComparer.cs index aa8e4287d0..fc908b48d1 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptorOrderComparer.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterDescriptorOrderComparer.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Mvc { if (x.Filter.Order == y.Filter.Order) { - return x.Origin.CompareTo(y.Origin); + return x.Scope.CompareTo(y.Scope); } else { diff --git a/src/Microsoft.AspNet.Mvc.Core/Filters/FilterOrigin.cs b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterScope.cs similarity index 84% rename from src/Microsoft.AspNet.Mvc.Core/Filters/FilterOrigin.cs rename to src/Microsoft.AspNet.Mvc.Core/Filters/FilterScope.cs index f632b40e10..4c133ba262 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Filters/FilterOrigin.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Filters/FilterScope.cs @@ -1,6 +1,6 @@ namespace Microsoft.AspNet.Mvc { - public static class FilterOrigin + public static class FilterScope { public static readonly int Action = 100; public static readonly int Controller = 200; diff --git a/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs b/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs index c8bf074a42..1b872fb3b2 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ReflectedActionDescriptorProvider.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Mvc _parameterDescriptorFactory = parameterDescriptorFactory; var filters = globalFilters ?? Enumerable.Empty(); - _globalFilters = filters.Select(f => new FilterDescriptor(f, FilterOrigin.Global)); + _globalFilters = filters.Select(f => new FilterDescriptor(f, FilterScope.Global)); } public int Order @@ -51,7 +51,7 @@ namespace Microsoft.AspNet.Mvc var controllerAttributes = cd.ControllerTypeInfo.GetCustomAttributes(inherit: true).ToArray(); var globalAndControllerFilters = controllerAttributes.OfType() - .Select(filter => new FilterDescriptor(filter, FilterOrigin.Controller)) + .Select(filter => new FilterDescriptor(filter, FilterScope.Controller)) .Concat(_globalFilters) .OrderBy(d => d, FilterDescriptorOrderComparer.Comparer) .ToArray(); @@ -112,7 +112,7 @@ namespace Microsoft.AspNet.Mvc var attributes = methodInfo.GetCustomAttributes(inherit: true).ToArray(); - var filtersFromAction = attributes.OfType().Select(filter => new FilterDescriptor(filter, FilterOrigin.Action)); + var filtersFromAction = attributes.OfType().Select(filter => new FilterDescriptor(filter, FilterScope.Action)); ad.FilterDescriptors = filtersFromAction.Concat(globalAndControllerFilters) .OrderBy(d => d, FilterDescriptorOrderComparer.Comparer)