为什么在Razor中使用Html.RenderAction时会出现StackOverflowException?

Eri*_*oom 6 stack-overflow asp.net-mvc razor

我正在将WebForms应用程序转换为Razor,除非我尝试使用,否则一切正常Html.RenderAction.每当我打电话给这个,我就会得到一个StackOverflowException.有没有人知道可能导致这种情况的原因?

我的操作模板如下所示:

@model dynamic   

should be rendering this
Run Code Online (Sandbox Code Playgroud)

在我的_Layout.cshtml文件中,我渲染这样的动作:

@{Html.RenderAction("MyPartialAction");}
Run Code Online (Sandbox Code Playgroud)

我的_ViewStart.cshtml文件如下:

@{
    this.Layout = "~/Views/Shared/_Layout.cshtml";
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*oom 18

问题是您的操作模板没有定义要使用的布局.因此,它会自动获取_ViewStart.cshtml文件中指定的那个.这实际上将导致嵌套在自身的_Layout.cshtml文件循环往复.因此StackOverflowException.解决方案很简单.将操作模板中的布局设置为null:

@model dynamic
@{
   Layout = null;
}
should be rendering this
Run Code Online (Sandbox Code Playgroud)

现在模板不会请求嵌入到布局文件中,一切正常.