asp.net-mvc 为什么在 Asp.net MVC 2 中的子操作中不允许重定向结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2056421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:02:34  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-2theory

提问by Sruly

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 Futures RenderAction 方法呈现的部分操作。其中一些在其中的表单被处理后执行重定向。

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

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

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.

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

Any one know the reason for this limitation?

有谁知道这个限制的原因?

Thanks

谢谢

回答by Levi

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.

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

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.

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

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.

在您的特定情况下,外部操作应该重定向。如果您最终只是从视图内部重定向而不向用户显示任何内容,那么首先确实没有目的通过视图,因为外部操作可以适当地将工作委派给它自己的。

回答by ybigus

Try to use something like this in Child Action:

尝试在 Child Action 中使用类似的东西:

ControllerContext.HttpContext.Response.Redirect(ControllerContext.HttpContext.Request.Url.ToString());

回答by Vladimir

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>

回答by Jason Barile

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?

就我而言,呈现的表单是我正在构建的网站的扩展上的“配置”面板。我希望扩展自己的控制器能够处理表单处理,然后重定向回列出所有配置扩展的管理页面。我认为在这里要求父页面的控制器处理扩展表单是不合适或不切实际的。你建议我做什么?

回答by Jonathan

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.

在我不寻常的情况下,我有一个自定义 AuthorizeAttribute 附加到我的控制器,它试图重定向子操作,这是(如上所述)不允许的。

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 ..

回答by gandil

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

回答by aaimnr

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.

在这种情况下,只需将部分视图表单的提交 url 指向作为问题重定向目标的操作,并让它自行执行重定向到其 GET 版本。