13

I have this error:

Error executing child request for handler '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();
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • You may look this question http://stackoverflow.com/questions/2056421/why-are-redirect-results-not-allowed-in-child-actions-in-asp-net-mvc-2 – Tassadaque Jan 19 '12 at 10:38

7 Answers7

12

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

Doug
  • 14,387
  • 17
  • 74
  • 104
  • In my case it wasn't actually `[RequireHttps]`, but another (custom) attribute which also happens to do some redirecting. This answer got me thinking about the right thing though. Thanks! – OutstandingBill Nov 20 '19 at 01:42
6

Instead of @Html use @Url.

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

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

Dumisani
  • 2,988
  • 1
  • 29
  • 40
6

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.

You can return RedirectToAction instead.

dknaack
  • 60,192
  • 27
  • 155
  • 202
  • I tried so many solutions to get a custom auth filter working and finally figured out that my view was calling `@Html.Action()` instead of `@Url.Action()` the latter gives you a url for a link and the former executes a controller action rendering it's view in place. Very frustrating, make sure you aren't making this mistake – Bron Davies Apr 03 '15 at 07:12
  • I am using RedirectToAction in my controller, but it still gives me the error – Denny Oct 04 '17 at 08:37
1

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.

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:

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

3)Defined custom https attribute:

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]

Taras Pelenio
  • 73
  • 2
  • 9
0

redirect like this

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

instead of

return redirect("Action","Controller")
Mohammad Hassani
  • 511
  • 6
  • 14
0

remove the [NoDirectAccess] annotation if added in the controller.

and in controller for the partial view

return PartialView() instead of return View()

Bhushan Shimpi
  • 90
  • 1
  • 10
0

This happens in cases where the Action called by the Html.Action performs a redirection such as

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

instead of returning the desired partialView

So the solution would be to remove the redirection.

Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44