Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2

Tags:

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

like image 290
Sruly Avatar asked Jan 13 '10 12:01

Sruly


People also ask

What is redirect result in MVC?

RedirectResult. RedirectResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header to the supplied URL. It will redirect us to the provided URL, it doesn't matter if the URL is relative or absolute.

What is difference between redirect and RedirectToAction?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.

Which method is used to redirect to another action method?

The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML.

What is used to redirect from one action to another in MVC core?

Use RedirectToActionResult in ASP.NET Core MVC This action result can be used to redirect to the specified action and controller. If no controller is specified it redirects to the specified action within the current controller.


2 Answers

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.

like image 102
Levi Avatar answered Sep 19 '22 00:09

Levi


Try to use something like this in Child Action:

ControllerContext.HttpContext.Response.Redirect(ControllerContext.HttpContext.Request.Url.ToString()); 
like image 31
ybigus Avatar answered Sep 22 '22 00:09

ybigus