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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8924529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:12:16  来源:igfitidea点击:

"Child actions are not allowed to perform redirect actions"

c#asp.net-mvc-3

提问by Sachin Kainth

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();
}

采纳答案by dknaack

This is not allowed because MVC has already started Rendering the View to the browser (client). So the MVC Frameworks blocks this, because the client already receives data (html). As long as the rendering is in progress you not able to redirect in your child view.

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

You can return RedirectToActioninstead.

您可以返回RedirectToAction

回答by Doug

This happened to me because I had [RequireHttps] on the Controller, and a child action was called from a different controller. The RequireHttps attribute caused the redirect

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

回答by Taras Pelenio

I had same situation like Doug described above

我有和上面描述的道格一样的情况

My solution: 1)Created custom Controller Factory. It's need for getting ControllerContext in my custom https attribute.

我的解决方案: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)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]

回答by Dumisani

Instead of

代替

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

Use

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

Worked for me :)

为我工作:)

回答by Mohammad Hassani

redirect like this

像这样重定向

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

instead of

代替

return redirect("Action","Controller")

回答by Bhushan Shimpi

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()