Why redirect results are not allowed in child activities in Asp.net MVC 2

I have partial actions that I perform using the Asp.Net Futures RenderAction method. Some of them perform redirects after processing forms in them.

Now that I upgraded to Asp.Net MVC 2 RC, he gave me the error message "No child actions allowed to perform redirection actions."

I checked the source code and I found a line that throws an exception. To get around this, I can create a custom RedirectResult, but before I do this, I want to understand why the infrastructure does not allow this in the first place. There must be a good reason, and maybe I shouldn't do that either.

Does anyone know the reason for this limitation?

thank

+21
theory asp.net-mvc asp.net-mvc-2
Jan 13 '10 at
source share
7 answers

The limitation exists because MVC has already begun rendering the presentation to the client. The redirect effect from this point is undefined. It can work perfectly, it can continue rendering the original view without redirecting, it can throw another exception, etc.

Since the result of this action is undefined, the structure blocks it. In practice, RenderAction should never be used to create anything other than a view (or similar view) for the same reasons.

In your particular case, the external action should be redirected. If you are just going to end up redirecting from the view anyway without showing anything to the user, then there really was no purpose to get through the review in the first place, since an external action could delegate the work properly to it on its own.

+32
Jan 14 '10 at 3:23
source share

Try using something like this in Action Action:

ControllerContext.HttpContext.Response.Redirect(ControllerContext.HttpContext.Request.Url.ToString()); 
+17
Mar 26 '11 at 17:48
source share

My decision.

Action method:

 return View("Redirect", model); 

View:

 <script type="text/javascript" language="javascript"> document.location = '<%: Url.Action("Index", "Album", new { id = Model.Id }) %>';</script> 
+12
Feb 15 '11 at 2:56 a.m.
source share

In my case, the form displayed is a “configure” panel on the extension for the website I am creating. I would like my own extension controller to be able to handle form processing and then redirect back to the administration page with a list of all configured extensions. I do not think it is advisable or practical here to ask the parent page controller to process the form for the extension. What would you suggest instead?

+6
Jan 28 '10 at 21:15
source share

In my unusual case, I had a special AuthorizeAttribute attribute attached to my controllers that tried to redirect to a child action that (as mentioned above) is not allowed.

To fix the problem, I removed the authorization redirection to 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 .. 
+5
Oct 18 '13 at 11:36 on
source share

Sometimes this error occurred while trying to perform the action of the base result. Example:

 ActionResult X Return View View X RenderAction Y ActionResult Y // Bla bla return View // else return RedirectToAction X 
+3
Jan 20 '11 at 12:35
source share

In this case, simply indicate the form for sending part of the view to the action that was the target of your problem redirect, and let it redirect to its version of GET.

+1
Jan 18
source share



All Articles