简体   繁体   English

“不允许子操作执行重定向操作”

[英]“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.

相关问题 子操作不允许执行重定向操作 - Child actions are not allowed to perform redirect actions 错误:子操作不允许执行重定向操作 - Error: Child actions are not allowed to perform redirect actions 子操作不允许执行重定向操作错误 - Child actions are not allowed to perform redirect actions error 子操作不允许执行重定向操作。 (使用PartialViews) - Child actions are not allowed to perform redirect actions. (Using PartialViews) 子操作不允许执行重定向操作MVC4 - Child actions are not allowed to perform redirect actions MVC4 ASP.NET MVC 3 + Razor错误:不允许子操作执行重定向操作 - ASP.NET MVC 3 + Razor Error: Child actions are not allowed to perform redirect actions 在部分视图控制器中无法正常工作。(错误 - 不允许子操作执行重定向操作。) - doesn't work redirecttoaction in partial view controller.(Error - Child actions are not allowed to perform redirect actions.) 如何解决不允许子操作执行重定向操作,其他答案不解决 - How to fix Child actions are not allowed to perform redirect actions, other answers does not fix 不允许子操作执行重定向操作 - 使用ASP.NET MVC 2和Razor时出错 - Child actions are not allowed to perform redirect actions - error while working with ASP.NET MVC 2 and Razor 重定向到区域操作中的操作 - Redirect to action in area actions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM