【问题标题】:"Child actions are not allowed to perform redirect actions"“不允许子动作执行重定向动作”
【发布时间】:2022-01-14 20:59:39
【问题描述】:

我有这个错误:

执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。

内部异常:

不允许子动作执行重定向动作。

知道为什么会这样吗?

顺便说一句,错误发生在这一行:

@Html.Action("Menu", "Navigation")

导航控制器中的菜单操作如下所示:

public ActionResult Menu()
{
    return PartialView();
}

【问题讨论】:

标签: c# asp.net-mvc-3


【解决方案1】:

这发生在我身上,因为我在控制器上有 [RequireHttps],并且从不同的控制器调用了一个子操作。 RequireHttps 属性导致了重定向

【讨论】:

  • 在我的例子中,它实际上不是[RequireHttps],而是另一个(自定义)属性,它也恰好做了一些重定向。这个答案让我想到了正确的事情。谢谢!
【解决方案2】:

这是不允许的,因为 MVC 已经开始将视图呈现给浏览器(客户端)。 所以 MVC 框架阻止了这一点,因为客户端已经接收到数据(html)。只要渲染正在进行,您就无法在子视图中重定向。

您可以改为返回RedirectToAction

【讨论】:

  • 我尝试了很多解决方案来让自定义身份验证过滤器正常工作,最后发现我的视图是调用 @Html.Action() 而不是 @Url.Action() 后者为您提供链接的 url,前者执行一个控制器动作呈现它的视图。非常令人沮丧,请确保您没有犯此错误
  • 我在控制器中使用 RedirectToAction,但它仍然给我错误
【解决方案3】:

@Url代替@Html

之前:@Html.Action("Menu", "Navigation")

之后:@Url.Action("Menu", "Navigation")

【讨论】:

    【解决方案4】:

    我遇到了和上面描述的道格一样的情况

    我的解决方案: 1)创建自定义控制器工厂。需要在我的自定义 https 属性中获取 ControllerContext 。

    public class CustomControllerFactory : DefaultControllerFactory
        {
            public override IController CreateController(RequestContext requestContext, string controllerName)
            {
                var controller = base.CreateController(requestContext, controllerName);
                HttpContext.Current.Items["controllerInstance"] = controller;
                return controller;
            }
        }
    }
    

    2)在 Global.asax 文件中的 Application_Start 函数中写下:

    ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
    

    3)定义自定义https属性:

    public class CustomRequireHttpsAttribute : System.Web.Mvc.RequireHttpsAttribute
        {
            public bool RequireSecure = false;
    
            public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
            {
    
                if (RequireSecure && !((Controller)HttpContext.Current.Items["controllerInstance"]).ControllerContext.IsChildAction)
                {
                    base.OnAuthorization(filterContext);
                }
            }        
        } 
    

    4) 使用新属性定义帐户控制器: [CustomRequireHttps]

    【讨论】:

      【解决方案5】:

      像这样重定向

      string returnUrl = Request.UrlReferrer.AbsoluteUri;
      return Redirect(returnUrl);
      

      而不是

      return redirect("Action","Controller")
      

      【讨论】:

        【解决方案6】:

        如果添加在控制器中,请删除 [NoDirectAccess] 注释。

        在局部视图的控制器中

        返回 PartialView() 而不是返回 View()

        【讨论】:

          【解决方案7】:

          这种情况发生在由 Html.Action 调用的 Action 执行重定向的情况下,例如

          return RedirectToAction("Error", "Home");
          

          而不是返回所需的partialView

          所以解决方案是删除重定向。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-03
            • 1970-01-01
            相关资源
            最近更新 更多