CR Feedback, add ordering sample to the MVCSample.
null result handling in ActionResultFilterEndPoint
This commit is contained in:
parent
8ff65267f9
commit
6f0151ba5e
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,8 @@ namespace MvcSample
|
||||||
[ServiceFilter(typeof(PassThroughAttribute))]
|
[ServiceFilter(typeof(PassThroughAttribute))]
|
||||||
[PassThrough(Order = 0)]
|
[PassThrough(Order = 0)]
|
||||||
[PassThrough(Order = 2)]
|
[PassThrough(Order = 2)]
|
||||||
|
[InspectResultPage]
|
||||||
|
[UserNameProvider(Order = -1)]
|
||||||
public class FiltersController : Controller
|
public class FiltersController : Controller
|
||||||
{
|
{
|
||||||
private readonly User _user = new User() { Name = "User Name", Address = "Home Address" };
|
private readonly User _user = new User() { Name = "User Name", Address = "Home Address" };
|
||||||
|
|
@ -15,9 +17,13 @@ namespace MvcSample
|
||||||
// TODO: Add a real filter here
|
// TODO: Add a real filter here
|
||||||
[ServiceFilter(typeof(PassThroughAttribute))]
|
[ServiceFilter(typeof(PassThroughAttribute))]
|
||||||
[AgeEnhancer]
|
[AgeEnhancer]
|
||||||
[InspectResultPage]
|
public IActionResult Index(int age, string userName)
|
||||||
public IActionResult Index(int age)
|
|
||||||
{
|
{
|
||||||
|
if (!string.IsNullOrEmpty(userName))
|
||||||
|
{
|
||||||
|
_user.Name = userName;
|
||||||
|
}
|
||||||
|
|
||||||
_user.Age = age;
|
_user.Age = age;
|
||||||
|
|
||||||
return View("MyView", _user);
|
return View("MyView", _user);
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,12 @@ namespace Microsoft.AspNet.Mvc.Filters
|
||||||
{
|
{
|
||||||
public async Task Invoke(ActionResultFilterContext context, Func<Task> next)
|
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);
|
await context.Result.ExecuteResultAsync(context.ActionContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Filters
|
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 class AuthorizationFilterEndPoint : IAuthorizationFilter
|
||||||
{
|
{
|
||||||
public bool EndPointCalled { get; private set; }
|
public bool EndPointCalled { get; private set; }
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,13 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
public class FilterDescriptor
|
public class FilterDescriptor
|
||||||
{
|
{
|
||||||
public FilterDescriptor([NotNull]IFilter filter, int origin)
|
public FilterDescriptor([NotNull]IFilter filter, int filterScope)
|
||||||
{
|
{
|
||||||
Filter = filter;
|
Filter = filter;
|
||||||
Origin = origin;
|
Scope = filterScope;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IFilter Filter { get; private set; }
|
public IFilter Filter { get; private set; }
|
||||||
public int Origin { get; private set; }
|
public int Scope { get; private set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
if (x.Filter.Order == y.Filter.Order)
|
if (x.Filter.Order == y.Filter.Order)
|
||||||
{
|
{
|
||||||
return x.Origin.CompareTo(y.Origin);
|
return x.Scope.CompareTo(y.Scope);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
public static class FilterOrigin
|
public static class FilterScope
|
||||||
{
|
{
|
||||||
public static readonly int Action = 100;
|
public static readonly int Action = 100;
|
||||||
public static readonly int Controller = 200;
|
public static readonly int Controller = 200;
|
||||||
|
|
@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
_parameterDescriptorFactory = parameterDescriptorFactory;
|
_parameterDescriptorFactory = parameterDescriptorFactory;
|
||||||
var filters = globalFilters ?? Enumerable.Empty<IFilter>();
|
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
|
public int Order
|
||||||
|
|
@ -51,7 +51,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
var controllerAttributes = cd.ControllerTypeInfo.GetCustomAttributes(inherit: true).ToArray();
|
var controllerAttributes = cd.ControllerTypeInfo.GetCustomAttributes(inherit: true).ToArray();
|
||||||
var globalAndControllerFilters =
|
var globalAndControllerFilters =
|
||||||
controllerAttributes.OfType<IFilter>()
|
controllerAttributes.OfType<IFilter>()
|
||||||
.Select(filter => new FilterDescriptor(filter, FilterOrigin.Controller))
|
.Select(filter => new FilterDescriptor(filter, FilterScope.Controller))
|
||||||
.Concat(_globalFilters)
|
.Concat(_globalFilters)
|
||||||
.OrderBy(d => d, FilterDescriptorOrderComparer.Comparer)
|
.OrderBy(d => d, FilterDescriptorOrderComparer.Comparer)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
@ -112,7 +112,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
var attributes = methodInfo.GetCustomAttributes(inherit: true).ToArray();
|
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)
|
ad.FilterDescriptors = filtersFromAction.Concat(globalAndControllerFilters)
|
||||||
.OrderBy(d => d, FilterDescriptorOrderComparer.Comparer)
|
.OrderBy(d => d, FilterDescriptorOrderComparer.Comparer)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue