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

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

 

posted on   新西兰程序员  阅读(1330)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示