23

I have some partial actions that I render with the Asp.Net Futures RenderAction method. Some of these perform redirects after the forms in them have been processed.

Now that I upgraded to Asp.Net MVC 2 RC it gives me an error "Child actions are not allowed to perform redirect actions".

I checked out the source code and I found the line that throws the exception. To Get around it I can make a custom RedirectResult, But before I do I want to understand why the framework doesn't allow it in the first place. There must be a good reason and maybe I Shouldn't do either.

Any one know the reason for this limitation?

Thanks

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
Sruly
  • 10,200
  • 6
  • 34
  • 39

7 Answers7

36

The limitation exists because MVC has already started rendering a view to the client. The effect of redirecting from this point is undefined. It could work perfectly, it could continue rendering the original view without redirecting, it could throw a different exception, etc.

Since the result of performing this action is undefined, the framework blocks it. In practice, RenderAction should never be used to render anything other than a view (or view-like content) for similar reasons.

In your particular case, the outer action should redirect. If you're just going to end up redirecting from within the view anyway without showing anything to the user, then there was really no purpose to going through the view in the first place, as the outer action could have delegated the work appropriately on its own.

Levi
  • 32,628
  • 3
  • 87
  • 88
17

Try to use something like this in Child Action:

ControllerContext.HttpContext.Response.Redirect(ControllerContext.HttpContext.Request.Url.ToString());
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
ybigus
  • 171
  • 1
  • 2
  • 1
    I used this in a login action displayed as partial view of the main page that needs to redirect on successful login – user1760527 Nov 29 '14 at 11:31
  • This actually causes the client browser to re-request the page after receiving the redirect header, but it works well enough. – Matthew Lock Feb 13 '17 at 03:16
13

My solution.

Action method:

return View("Redirect", model);

View:

<script type="text/javascript" language="javascript">
    document.location = '<%: Url.Action("Index", "Album", new { id = Model.Id }) %>';</script>
Leri
  • 12,367
  • 7
  • 43
  • 60
Vladimir
  • 1,630
  • 2
  • 18
  • 28
6

In my case, the form being rendered is a "configure" panel on an extension to a website I'm building. I'd like the extension's own controller to be able to handle the form processing and then redirect back to the admin page listing all configured extensions. I don't think it's appropriate or practical here to ask the parent page's controller to process the form for the extension. What would you suggest I do instead?

Jason Barile
  • 936
  • 1
  • 6
  • 17
  • That's the same scenario I have. Since I am only using it on admin pages for extensibility I decided to keep the redirect in the child action controller. I wrote my own ChildActionRedirectResult and then made a ChildActionController base that overrides the Redirect methods to use my own result. I would not do this for front end usage. – Sruly Feb 03 '10 at 12:50
  • I ended up just returning a simple view result with a "Your changes were saved" message and a link back to the admin page for managing extensions. It's not as pretty, but it did the trick. – Jason Barile Feb 05 '10 at 19:18
5

In my unusual case, I had a custom AuthorizeAttribute attached to my controllers which was attempting to redirect on a child action, which is (as mentioned above) not allowed.

To resolve the issue, I removed authorisation checking redirection on all child actions:

Public Overrides Sub OnAuthorization(filterContext As AuthorizationContext)
    //Child actions cannot redirect anyway, so no need to check permissions.
    If filterContext.IsChildAction Then Exit Sub

    .. parent authorisation checks ..
Jonathan
  • 25,873
  • 13
  • 66
  • 85
3

Sometimes this error occured when you try to render an action of base action result. Example:

ActionResult X
    Return View
View X
    RenderAction Y

ActionResult Y
    // Bla bla
       return View 
    // else
       return RedirectToAction X
gandil
  • 5,398
  • 5
  • 24
  • 46
1

In that case just point the partial view form's submit url to action that was the target of your problematic redirection and let it perform itself redirection to its GET version.

aaimnr
  • 1,646
  • 1
  • 17
  • 31