[英]“Child actions are not allowed to perform redirect actions”
我有这个错误:
执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。
内部例外:
不允许子操作执行重定向操作。
知道为什么会这样吗?
顺便说一句,错误发生在这一行:
@Html.Action("Menu", "Navigation")
导航控制器中的菜单操作如下所示:
public ActionResult Menu()
{
return PartialView();
}
这发生在我身上,因为我在控制器上有 [RequireHttps],并且从不同的控制器调用了一个子操作。 RequireHttps 属性导致重定向
这是不允许的,因为 MVC 已经开始将视图渲染到浏览器(客户端)。 所以 MVC 框架阻止了这一点,因为客户端已经接收到数据 (html)。 只要渲染正在进行中,您就无法在子视图中重定向。
您可以改为返回RedirectToAction
。
代替
@Html.Action("Menu", "Navigation")
采用
@Url.Action("Menu", "Navigation")
为我工作:)
我有和上面描述的道格一样的情况
我的解决方案: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]
像这样重定向
string returnUrl = Request.UrlReferrer.AbsoluteUri;
return Redirect(returnUrl);
代替
return redirect("Action","Controller")
如果在控制器中添加了 [NoDirectAccess] 注释,请删除。
并在局部视图的控制器中
返回 PartialView() 而不是 return View()
在 Html.Action 调用的 Action 执行重定向的情况下会发生这种情况,例如
return RedirectToAction("Error", "Home");
而不是返回所需的partialView
所以解决方案是删除重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.