[英]“Child actions are not allowed to perform redirect actions”
I have this error:我有这个错误:
Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper”的子请求时出错。
with inner exception:内部例外:
Child actions are not allowed to perform redirect actions.
不允许子操作执行重定向操作。
Any idea why this happening?知道为什么会这样吗?
Incidentally, the error is happening on this line:顺便说一句,错误发生在这一行:
@Html.Action("Menu", "Navigation")
The Menu Action in the Navigation Controller looks like this:导航控制器中的菜单操作如下所示:
public ActionResult Menu()
{
return PartialView();
}
This happened to me because I had [RequireHttps] on the Controller, and a child action was called from a different controller.这发生在我身上,因为我在控制器上有 [RequireHttps],并且从不同的控制器调用了一个子操作。 The RequireHttps attribute caused the redirect
RequireHttps 属性导致重定向
This is not allowed because MVC has already started Rendering the View to the browser (client).这是不允许的,因为 MVC 已经开始将视图渲染到浏览器(客户端)。 So the MVC Frameworks blocks this, because the client already receives data (html).
所以 MVC 框架阻止了这一点,因为客户端已经接收到数据 (html)。 As long as the rendering is in progress you not able to redirect in your child view.
只要渲染正在进行中,您就无法在子视图中重定向。
You can return RedirectToAction
instead.您可以改为返回
RedirectToAction
。
Instead of代替
@Html.Action("Menu", "Navigation")
Use采用
@Url.Action("Menu", "Navigation")
Worked for me :)为我工作:)
I had same situation like Doug described above
我有和上面描述的道格一样的情况
My solution: 1)Created custom Controller Factory.我的解决方案:1)创建自定义控制器工厂。 It's need for getting ControllerContext in my custom https attribute.
需要在我的自定义 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)In Application_Start function from Global.asax file wrote next: 2)在 Global.asax 文件中的 Application_Start 函数中,接下来写道:
ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
3)Defined custom https attribute: 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)Using new attribute for definition of account controller: [CustomRequireHttps]
4)使用新属性定义帐户控制器:
[CustomRequireHttps]
redirect like this像这样重定向
string returnUrl = Request.UrlReferrer.AbsoluteUri;
return Redirect(returnUrl);
instead of代替
return redirect("Action","Controller")
remove the [NoDirectAccess] annotation if added in the controller.如果在控制器中添加了 [NoDirectAccess] 注释,请删除。
and in controller for the partial view并在局部视图的控制器中
return PartialView() instead of return View()返回 PartialView() 而不是 return View()
This happens in cases where the Action called by the Html.Action performs a redirection such as在 Html.Action 调用的 Action 执行重定向的情况下会发生这种情况,例如
return RedirectToAction("Error", "Home");
instead of returning the desired partialView
而不是返回所需的
partialView
So the solution would be to remove the redirection.所以解决方案是删除重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.