CR Feedback, add ordering sample to the MVCSample.

null result handling in ActionResultFilterEndPoint
This commit is contained in:
Yishai Galatzer 2014-03-10 22:09:36 -07:00
parent 8ff65267f9
commit 6f0151ba5e
8 changed files with 51 additions and 11 deletions

View File

@ -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<Task> 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();
}
}
}

View File

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

View File

@ -8,6 +8,12 @@ namespace Microsoft.AspNet.Mvc.Filters
{
public async Task Invoke(ActionResultFilterContext context, Func<Task> 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);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Mvc
_parameterDescriptorFactory = parameterDescriptorFactory;
var filters = globalFilters ?? Enumerable.Empty<IFilter>();
_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<IFilter>()
.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<IFilter>().Select(filter => new FilterDescriptor(filter, FilterOrigin.Action));
var filtersFromAction = attributes.OfType<IFilter>().Select(filter => new FilterDescriptor(filter, FilterScope.Action));
ad.FilterDescriptors = filtersFromAction.Concat(globalAndControllerFilters)
.OrderBy(d => d, FilterDescriptorOrderComparer.Comparer)