public override void OnActionExecuting(ActionExecutingContext filterContext) { // use the extension method in your filter if (filterContext.HasAttribute(typeof(AllowAnonymousAttribute))) { // exit early... return; } // ...or do whatever else you need to do if (user == null || !user.Active) { filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Home" }, { "action", "NotAuthorized" } }); } base.OnActionExecuting(filterContext); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var result = await next(); // 若有异常,不应该提交数据 if (result.Exception != null) { return; } if (!context.HasAttribute <IgnoreUnitOfWork>()) { var unitOfWork = context.HttpContext.RequestServices.GetService <IUnitOfWork>(); if (unitOfWork != null) { await unitOfWork.CommitAsync(); } _logger.LogDebug("Executed unit of work filter"); } }