为什么在 Asp.net MVC 2 中的子操作中不允许重定向结果 [英] Why are Redirect Results not allowed in Child Actions in Asp.net MVC 2

查看:33
本文介绍了为什么在 Asp.net MVC 2 中的子操作中不允许重定向结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Asp.Net Futures RenderAction 方法呈现了一些部分操作.其中一些在处理完其中的表单后执行重定向.

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.

现在我升级到了 Asp.Net MVC 2 RC,它给了我一个错误不允许子操作执行重定向操作".

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

我查看了源代码,发现了抛出异常的那一行.为了解决这个问题,我可以创建一个自定义的 RedirectResult,但在我这样做之前,我想先了解为什么框架不允许这样做.一定有很好的理由,也许我也不应该这样做.

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?

谢谢

推荐答案

存在限制是因为 MVC 已经开始向客户端呈现视图.从这一点重定向的效果是不确定的.它可以完美运行,可以继续渲染原始视图而不重定向,也可以抛出不同的异常等.

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.

由于执行此操作的结果未定义,因此框架会阻止它.实际上,出于类似原因,不应使用 RenderAction 渲染视图(或类似视图的内容)以外的任何内容.

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.

这篇关于为什么在 Asp.net MVC 2 中的子操作中不允许重定向结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆