asp.net-mvc - 具有热重载功能的 ASP.NET Core 6.0 : Error when manipulating ViewBag

标签 asp.net-mvc asp.net-core hot-reload

我有一个新的 ASP.NET Core 6.0 MVC 应用程序。

Controller 操作在 ViewBag 中写入一些数据。

当我更改某些 ViewBag 元素时,页面会在浏览器中重新加载,并出现以下错误:

RuntimeBinderException:“System.Dynamic.DynamicObject”不包含“test”的定义

步骤:

  1. 在 Controller 操作中:ViewBag.test="first test";
  2. 重建解决方案 -> 页面加载正常
  3. 更改为ViewBag.test="第二次测试";
  4. 页面自动重新加载,但出现错误
  5. 重建解决方案 -> 再次正常工作

这是我的 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/

相关文章:

javascript - webpack 热模块替换不起作用

c# - 如何将带有 “required” html 属性的自定义错误消息添加到 mvc 5 razor View 文本输入编辑器

asp.net-mvc - 为什么我的 MVC 应用程序中有两个 web.config 文件

json - NLog数据库参数+.NET Core : all-event-properties in JSON?

c# - 无法从程序集 'System.Data,版本 = 4.0.0.0 加载类型 'Microsoft.SqlServer.Server.SqlContext'

c# - 在 ASP.NET Core 中显示 SelectList 中的数据

css - ASP.NET MVC Razor 和 css block

powershell - 存在代码为 145 的手动运行容器

webpack - 使用带有代理(?)的 webpack-serve 进行热重载