[ASP.NET MVC] Child actions are not allowed to perform redirect

我在Umbraco平台下,用MVC(SurfaceController)开发时,遇到这个问题

MemberEdit是一个partial View

[HttpGet]
[ActionName("MemberEdit")]
 public ActionResult MemberEditGet()
  {
        if (Members.GetCurrentLoginStatus().IsLoggedIn)
         {

                var currentProfileModel = new Gallagher.Fuelsystems.Core.Models.ProfileModel()
                {

                   // some codes here
                };

                return PartialView("MemberEdit", currentProfileModel);

            }

            else
            {
                return PartialView("MemberLogin");

                //return Redirect("/Login");
            }

        } 

刚开始,在else语句中,我使用的是 return Redirect("/Login"); 就报这个错误,后来我改为 return PartialView("MemberLogin"); 就没问题了

在网上搜索了关于这个错误信息,解释大概如下:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.

The redirect info is stored in response header. However, the response is already being sent when child action is run so headers can‘t be written.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

时间: 2024-12-28 02:44:11

[ASP.NET MVC] Child actions are not allowed to perform redirect的相关文章

深入理解ASP.NET MVC(6)

系列目录 Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionName)) 在深入研究调用过程的细节前,先有一个总体的认识是很有帮助的.InvokeAction方法大致是按照这样的顺序进行的: 查找action:MVC内部查找action的方法似乎有点复杂,涉及到一个ActionDescriptor的东西,但是原理上是通过反射,在以后的文章中会有所涉及.

Professional C# 6 and .NET Core 1.0 - Chapter 41 ASP.NET MVC

What's In This Chapter? Features of ASP.NET MVC 6 Routing Creating Controllers Creating Views Validating User Inputs Using Filters Working with HTML and Tag Helpers Creating Data-Driven Web Applications Implementing Authentication and Authorization W

ASP.NET MVC 4 (五) 视图

视图引擎与视图 多数情况下控制器action方法返回ViewResult对象,MVC内建action调用器ControllerActionInvoker负责调用控制器action方法并调用视图引擎处理ViewResut,由视图引擎将ViewResult转化为ViewEngineResult对象,ViewEngineResult对象内含实现IView接口的视图对象,最终MVC框架调用视图对象的Render的方法渲染输出结果.下面还是以例子来演示这个过程,先来看看相关接口和类的定义与实现: publ

[Asp.net mvc]OutputCacheAttribute

什么是Cache? 缓存在web应用中是一种以空间换去时间的技术,把频繁访问并且不经常变化的数据存储到内存中,以达到快速访问的目的.在web应用中是比较常见的优化方式. OutputCacheAttribute 表示一个特性,该特性用于标记将缓存其输出的操作方法. OutpuCacheAttribute定义 代码片段 [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true,

ASP.NET MVC & Web API Brief Introduction

Pure Web Service(ASMX): Starting back in 2002 with the original release of .NET, a developer could fairly easily create an ASP.NETASMX-based XML web service that allowed other .NET and non-.NET clients to call it.Those web services implemented variou

asp.net mvc 页面传值的方法总结

转自:http://msprogrammer.serviciipeweb.ro/2012/01/15/usual-methods-to-transfer-data-from-page-to-page-in-asp-net-mvc/ Usual Methods to transfer data from Page To Page in ASP.NET MVC Preamble: In ASP.NET ( like in PHP and other Web frameworks) there are

关于AJAX跨域调用ASP.NET MVC或者WebAPI服务的问题及解决方案

原文:http://www.cnblogs.com/chenxizhang/p/3821703.html 问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP.NET Web API编写的服务时,会发生无法访问的情况. 重现方式 1.使用模板创建一个最简单的ASP.NET Web API项目,调试起来确认能正常工作 public class TestController : ApiController { // GET api/test public IEnumera

ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebGrid is a very powerful HTML helper component introduced with ASP.NET MVC 3 and ASP.NET Web Pages. Despite all its cool features, it is troublesome to

Ninject之旅之十三:Ninject在ASP.NET MVC程序上的应用

摘要: 在Windows客户端程序(WPF和Windows Forms)中使用Ninject和在控制台应用程序中使用Ninject没什么不同.在这些应用程序里我们不需要某些配置用来安装Ninject,因为在Windows客户端应用程序里,开发者可以控制UI组件的实例化(Forms或Windows),可以很容易地委托这种控制到Ninject.然而在Web应用程序里,就不同了,因为框架负责了实例化UI元素.因此,我们需要知道怎样告诉框架委托这种控制责任给Ninject.幸运的是,让ASP.NET M