Feedback: Make common base class for all contexts and all of them expose FilterItem collection
ExtensionMethod will now apply to the base FilterContext class
This commit is contained in:
parent
adffc95b81
commit
8b6d39507d
|
|
@ -4,7 +4,7 @@ using Microsoft.AspNet.Mvc;
|
|||
|
||||
namespace MvcSample.Web.Filters
|
||||
{
|
||||
public class BlockAnonynous : AuthorizationFilterAttribute
|
||||
public class BlockAnonymous : AuthorizationFilterAttribute
|
||||
{
|
||||
public override async Task Invoke(AuthorizationFilterContext context, Func<Task> next)
|
||||
{
|
||||
|
|
@ -9,11 +9,11 @@ namespace MvcSample.Web.Filters
|
|||
{
|
||||
public async override Task Invoke(ActionResultFilterContext context, Func<Task> next)
|
||||
{
|
||||
ViewResult viewResult = context.Result as ViewResult;
|
||||
var viewResult = context.ActionResult as ViewResult;
|
||||
|
||||
if (viewResult != null)
|
||||
{
|
||||
User user = viewResult.ViewData.Model as User;
|
||||
var user = viewResult.ViewData.Model as User;
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace MvcSample.Web
|
|||
[PassThrough(Order = 0)]
|
||||
[PassThrough(Order = 2)]
|
||||
[InspectResultPage]
|
||||
[BlockAnonynous]
|
||||
[BlockAnonymous]
|
||||
[UserNameProvider(Order = -1)]
|
||||
public class FiltersController : Controller
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ using Microsoft.AspNet.Mvc.Filters;
|
|||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public static class AuthorizationFilterContextExtensions
|
||||
public static class FilterContextExtensions
|
||||
{
|
||||
public static bool HasAllowAnonymous([NotNull] this AuthorizationFilterContext context)
|
||||
public static bool HasAllowAnonymous([NotNull] this FilterContext context)
|
||||
{
|
||||
return context.FilterItems.Any(item => item.Filter is IAllowAnonymous);
|
||||
}
|
||||
|
|
@ -1,21 +1,18 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.Filters;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ActionFilterContext
|
||||
public class ActionFilterContext : FilterContext
|
||||
{
|
||||
public ActionFilterContext(ActionContext actionContext,
|
||||
IDictionary<string, object> actionArguments)
|
||||
public ActionFilterContext([NotNull] ActionContext actionContext,
|
||||
[NotNull] IReadOnlyList<FilterItem> filterItems,
|
||||
[NotNull] IDictionary<string, object> actionArguments)
|
||||
: base(actionContext, filterItems)
|
||||
{
|
||||
ActionContext = actionContext;
|
||||
ActionArguments = actionArguments;
|
||||
}
|
||||
|
||||
public virtual IDictionary<string, object> ActionArguments { get; private set; }
|
||||
|
||||
public virtual ActionContext ActionContext { get; private set; }
|
||||
|
||||
public virtual IActionResult Result { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Mvc.Filters
|
|||
context.ActionArguments);
|
||||
|
||||
var underlyingReturnType = TypeHelper.GetTaskInnerTypeOrNull(actionMethodInfo.ReturnType) ?? actionMethodInfo.ReturnType;
|
||||
context.Result = _actionResultFactory.CreateActionResult(
|
||||
context.ActionResult = _actionResultFactory.CreateActionResult(
|
||||
underlyingReturnType,
|
||||
actionReturnValue,
|
||||
context.ActionContext);
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
namespace Microsoft.AspNet.Mvc
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.Filters;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ActionResultFilterContext
|
||||
public class ActionResultFilterContext : FilterContext
|
||||
{
|
||||
public ActionResultFilterContext(ActionContext actionContext, IActionResult initialResult)
|
||||
public ActionResultFilterContext(ActionContext actionContext, IReadOnlyList<FilterItem> filterItems, IActionResult initialResult)
|
||||
: base(actionContext, filterItems)
|
||||
{
|
||||
ActionContext = actionContext;
|
||||
Result = initialResult;
|
||||
ActionResult = initialResult;
|
||||
}
|
||||
|
||||
public ActionContext ActionContext { get; private set; }
|
||||
|
||||
public IActionResult Result { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,12 +9,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)
|
||||
if (context.ActionResult == null)
|
||||
{
|
||||
context.Result = new EmptyResult();
|
||||
context.ActionResult = new EmptyResult();
|
||||
}
|
||||
|
||||
await context.Result.ExecuteResultAsync(context.ActionContext);
|
||||
await context.ActionResult.ExecuteResultAsync(context.ActionContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,21 @@
|
|||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.Filters;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class AuthorizationFilterContext
|
||||
public class AuthorizationFilterContext : FilterContext
|
||||
{
|
||||
private IActionResult _actionResult;
|
||||
|
||||
public AuthorizationFilterContext([NotNull] ActionContext actionContext, [NotNull] IReadOnlyList<FilterItem> filterItems)
|
||||
: base(actionContext, filterItems)
|
||||
{
|
||||
ActionContext = actionContext;
|
||||
FilterItems = filterItems;
|
||||
}
|
||||
|
||||
public bool HasFailed { get; private set; }
|
||||
|
||||
public ActionContext ActionContext { get; private set; }
|
||||
|
||||
public IReadOnlyList<FilterItem> FilterItems { get; private set; }
|
||||
|
||||
// Result
|
||||
public IActionResult ActionResult
|
||||
public override IActionResult ActionResult
|
||||
{
|
||||
get { return _actionResult; }
|
||||
set
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Filters
|
||||
{
|
||||
public class FilterContext
|
||||
{
|
||||
public FilterContext([NotNull] ActionContext actionContext, [NotNull] IReadOnlyList<FilterItem> filterItems)
|
||||
{
|
||||
ActionContext = actionContext;
|
||||
FilterItems = filterItems;
|
||||
}
|
||||
|
||||
public ActionContext ActionContext { get; private set; }
|
||||
public IReadOnlyList<FilterItem> FilterItems { get; private set; }
|
||||
|
||||
// Result
|
||||
public virtual IActionResult ActionResult { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -47,6 +47,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
Select(fd => new FilterItem(fd)).ToList());
|
||||
|
||||
_filterProvider.Invoke(filterProviderContext);
|
||||
var filterMetaItems = filterProviderContext.Result.ToArray();
|
||||
|
||||
PreArrangeFiltersInPipeline(filterProviderContext);
|
||||
|
||||
|
|
@ -72,7 +73,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
var authZEndPoint = new AuthorizationFilterEndPoint();
|
||||
_authorizationFilters.Add(authZEndPoint);
|
||||
|
||||
var authZContext = new AuthorizationFilterContext(_actionContext, filterProviderContext.Result.ToArray());
|
||||
var authZContext = new AuthorizationFilterContext(_actionContext, filterMetaItems);
|
||||
var authZPipeline = new FilterPipelineBuilder<AuthorizationFilterContext>(_authorizationFilters, authZContext);
|
||||
|
||||
await authZPipeline.InvokeAsync();
|
||||
|
|
@ -99,6 +100,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
var parameterValues = await GetParameterValues(modelState);
|
||||
|
||||
var actionFilterContext = new ActionFilterContext(_actionContext,
|
||||
filterMetaItems,
|
||||
parameterValues);
|
||||
|
||||
var actionEndPoint = new ReflectedActionFilterEndPoint(_actionResultFactory, controller);
|
||||
|
|
@ -109,12 +111,12 @@ namespace Microsoft.AspNet.Mvc
|
|||
|
||||
await actionFilterPipeline.InvokeAsync();
|
||||
|
||||
actionResult = actionFilterContext.Result;
|
||||
actionResult = actionFilterContext.ActionResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var actionResultFilterContext = new ActionResultFilterContext(_actionContext, actionResult);
|
||||
var actionResultFilterContext = new ActionResultFilterContext(_actionContext, filterMetaItems, actionResult);
|
||||
var actionResultFilterEndPoint = new ActionResultFilterEndPoint();
|
||||
_actionResultFilters.Add(actionResultFilterEndPoint);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue