简体   繁体   English

未处理的异常System.StackOVerflowException c#

[英]Unhandled exception System.StackOVerflowException c#

I wrote a piece of code that returns a random string sponsorname from a list of sponsors. 我写了一段代码,从赞助商列表中返回一个随机字符串sponsorname。 This sponsorname should be visible at each page, so I call the RandomSponsor method in the shared _layout view. 这个sponsameame应该在每个页面都可见,所以我在shared _layout视图中调用RandomSponsor方法。 This RandomSponsor method is based in the HomeController and has it's own view containing only a Html.Action 这个RandomSponsor方法基于HomeController,并且它自己的视图只包含一个Html.Action

And at that Html.Action the program returns an error: 在那个Html.Action上程序返回一个错误:

System.StackOverflowException {Cannot evaluate expression because the current thread is in a stack overflow state.}

This is my RandomSponsor method: 这是我的RandomSponsor方法:

    [HttpGet]   
[ChildActionOnly]
public ActionResult RandomSponsor()
{
    var model = service.getRandomSponsor();
    return PartialView("RandomSponsor", model);
}

RandomSponsor.cshtml, where the programs stops RandomSponsor.cshtml,程序停止

@Html.Action("RandomSponsor")

And my call in the shared layout page _Layout.cshtml: 我在共享布局页面_Layout.cshtml中的调用:

@Html.Action("RandomSponsor", "Home")

While i'm debugging i noticed that the RandomSponsor method goes to it's view, but because my Html.Action requests the function again, it's stuck in a loop. 虽然我正在调试我注意到RandomSponsor方法进入它的视图,但因为我的Html.Action再次请求该函数,它被卡在一个循环中。 I think I give the wrong parameter to the Html.Action in the RandomSponsor.cshtml view, but I dont know what is the correct one. 我想我给RandomSponsor.cshtml视图中的Html.Action提供了错误的参数,但我不知道什么是正确的。

Does anyone had a similar problem or knows how to fix this error, i'm all ears. 有没有人有类似的问题或知道如何解决这个错误,我都是耳朵。

Regards 问候

The problem is that it seems for RandomSponsor partial view you have set _Layout.chtml as layout, 问题是,对于RandomSponsor局部视图,您已将_Layout.chtml设置为布局,

在此输入图像描述

so you have this scenario: 所以你有这个场景:

_Layout.chtml calls RandomSponsor, RandomSponsor first load it's layout _Layout.chtml,
_Layout.chtml calls RandomSponsor, RandomSponsor first load it's layout _Layout.chtml....till stackoverflow

You need to put the actual HTML that you want to child action to render in its view. 您需要将要执行子动作的实际HTML放在其视图中。

It doesn't make sense to have the view recursively render its own action. 让视图以递归方式呈现自己的动作是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型为'System.StackOverflowException'的C#未处理异常 - C# unhandled exception of type 'System.StackOverflowException System.StackOverflowException未处理 - C#,. NET - System.StackOverflowException was unhandled - C#, .NET C#服务器套接字引发在system.drawing.dll中发生的'system.stackoverflowexception'类型的未处理异常 - C# Server Socket raises an unhandled exception of type 'system.stackoverflowexception' occurred in system.drawing.dll C# - 实体框架 - mscorlib.dll中发生未处理的“System.StackOverflowException”类型异常 - C# - Entity Framework - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll 发生类型为'System.StackOverflowException'的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred 类型为“System.StackOverflowException”的未处理异常 - An unhandled exception of type 'System.StackOverflowException' 发生类型为“System.StackOverflowException”的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred System.StackOverflowException C# - System.StackOverflowException C# System.StackOverflowException未处理 - System.StackOverflowException was unhandled C#中引发了类型为'System.StackOverflowException'的异常 - Exception of type 'System.StackOverflowException' was thrown in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM