【问题标题】:Is it possible to redirect to another action using a custom action filter?是否可以使用自定义操作过滤器重定向到另一个操作?
【发布时间】:2022-01-10 08:28:12
【问题描述】:

我不熟悉 ASP.NET Core 操作的自定义过滤器属性。 如果使用自定义方法过滤器不存在某些数据,我需要重定向到另一个操作。

这是我的尝试:

[AttributeUsage(AttributeTargets.Class| AttributeTargets.Method, AllowMultiple = false)]
public class IsCompanyExistAttribute: ActionFilterAttribute
{
    private readonly ApplicationDbContext context;

    public IsCompanyExistAttribute(ApplicationDbContext context)
    {
        this.context = context;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //base.OnActionExecuting(filterContext);
        if (context.Companies == null)
        {
            return RedirectToAction(actionName: "Msg", controllerName: "Account", 
                new { message = "You are not allowed to register, since the company data not exist !!!" });
        }
    }

我没有使用filterContext。当然,RedirectToAction 行显示为错误(带有红色下划线),因为它是 void 方法,而不是操作结果。正如我提到的,我不熟悉自定义过滤器。

有什么帮助吗?

【问题讨论】:

    标签: asp.net-core custom-attributes action-filter custom-action-filter


    【解决方案1】:

    是的,是的。只需将ActionExecutingContext 实例的Result 属性设置为您的RedirectToActionResult。它应该如下所示:

     public override void OnActionExecuting(ActionExecutingContext filterContext)
     {
         var controller = filterContext.Controller as ControllerBase; 
         if (context.Companies == null && controller != null)
         {
             filterContext.Result = controller.RedirectToAction(
                 actionName: "Msg", 
                 controllerName: "Account", 
                 new { message = "You are not allowed to register, since the company data not exist !!!" }
             );
         }
         base.OnActionExecuting(filterContext);
     }
    

    【讨论】:

    • 这是一个很好的解决方案,谢谢@Jeremy Caney。我加了一票,但我的声誉还不够。我收到此消息(感谢您的反馈!您需要至少 15 个声望才能投票,但您的反馈已被记录)。再次感谢你。但是是否可以为我提供具有构造函数的自定义属性的用法?因为当我像这样使用它时 [IsCompanyExist] 我得到红线错误并且错误说(没有参数给出与所需形式参数'context'的对应关系)
    • 谢谢@Jeremy Caney。我确实投了赞成票。如果您需要帮助使用,我想可以通过使用“TypeFilter”来解决具有依赖注入的自定义过滤器的使用,但我仍然不知道如何正确使用它 [TypeFilter(typeof(IsCompanyExistAttribute), Arguments =上下文)]
    • @MedoofromEgypt:感谢您指出依赖注入问题。虽然这不是您问题的重点,但这是我想问您的问题。由于属性的工作方式,您只能通过属性语法将编译时常量传递给它们。但是,还有其他方法,包括使用TypeFilter。查看以下 Stack Overflow 答案,详细了解如何将对象实例注入过滤器:How can I use Dependency Injection in a .Net Core ActionFilterAttribute?
    • @MedoofromEgypt:另外,通常情况下,我发现@Paul Hiles 在他的博客上提供了a really useful explanation of dependency injection options,其中详细介绍了如何调用每个选项,以及它们的权衡是什么。我肯定会读一读。它应该可以解决您对该问题的任何疑问。如果没有,请将其作为一个新问题发布,然后通过评论指向我,我会在有时间时尝试回答。
    • 非常感谢@Jeremy Caney,您的 cmets 帮助我找到了如何将自定义过滤器与构造函数一起使用。解决方案非常简单。我希望将它们添加到您的答案中,以使其成为可能关注的完整指南。您所做的第一步只是添加构造函数。第二步:我将服务添加到 StartUp services.AddScoped();第三步:我在 ActionResult 上面使用它,像这样 [ServiceFilter(typeof(IsCompanyExistAttribute))]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2022-08-17
    • 2012-06-12
    相关资源
    最近更新 更多