我有一个新的 ASP.NET Core 6.0 MVC 应用程序。
Controller 操作在 ViewBag 中写入一些数据。
当我更改某些 ViewBag 元素时,页面会在浏览器中重新加载,并出现以下错误:
RuntimeBinderException:“System.Dynamic.DynamicObject”不包含“test”的定义
步骤:
- 在 Controller 操作中:
ViewBag.test="first test";
- 重建解决方案 -> 页面加载正常
- 更改为
ViewBag.test="第二次测试";
- 页面自动重新加载,但出现错误
- 重建解决方案 -> 再次正常工作
这是我的 launchSettings.json:
"watch run": {
"commandName": "Executable",
"executablePath": "dotnet.exe",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "watch run",
"launchBrowser": true,
"hotReloadEnabled": true,
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
},
"applicationUrl": "https://localhost:7033;http://localhost:5033"
},
所有 Nuget 软件包均已更新。
最佳答案
您遇到了一个旨在通过 NET8 修复的错误 --> https://github.com/dotnet/runtime/issues/59494 (谢谢,理查德!)
当您使用VS2022的热重载功能时,可能会出现此问题。
快速修复:停止调试并重新启动应用程序。
关于asp.net-mvc - 具有热重载功能的 ASP.NET Core 6.0 : Error when manipulating ViewBag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70110036/