gpt4 book ai didi

asp.net-mvc - RenderSection 按要求设置为 :false, 但仍抛出 "Object reference not set to an instance of an object"

转载 作者:行者123 更新时间:2023-12-03 15:46:51 24 4
gpt4 key购买 nike

此代码应该将我的部分标记为可选。

_layout.cshtml 
@RenderSection("ViewStyles",false)

或者
@RenderSection("ViewStyles",required:false)

我两个都试了。

然而,它抛出了一个异常
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

我尝试在不需要使用 ViewStyles 部分的 View 中添加一个空部分
@section ViewStyles {  }

但我仍然得到同样的异常(exception)。

还有其他解决方法吗?

最佳答案

@Chris-Pratt 用他的详细回答引导我走向正确的方向。我想分享我的代码以提供帮助:

我的问题是 @RenderSection("Header", false) 回来“对象引用未设置到对象的实例”。单步抛出代码,它不会单步通过和中断:

header section

最后,我意识到是下一行代码被破坏了:

<body class="@TempData[AppConstants.GlobalClass].ToString() 
loggedin-@(User.Identity.Name.ToString().ToLower())">

@TempData 为空

关于asp.net-mvc - RenderSection 按要求设置为 :false, 但仍抛出 "Object reference not set to an instance of an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27023058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com