Visual Studio 2022中更新Bootstrap报错求助
Visual Studio 2022更新Bootstrap时脚本渲染报错的解决方案
问题描述
我在Visual Studio 2022中更新Bootstrap后,页面使用@Scripts.Render("~/bundles/bootstrap")时出现报错,相关代码如下:
@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") // 此处报错 @RenderSection("scripts", required: false)
报错截图:
解决步骤
检查BundleConfig配置
打开App_Start/BundleConfig.cs,确认~/bundles/bootstrap的配置是否正确指向更新后的Bootstrap脚本文件。Bootstrap 5+版本使用bootstrap.bundle.min.js(内置Popper),确保路径和文件名匹配:bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.bundle.min.js"));验证文件存在性
确认项目Scripts目录下有更新后的Bootstrap脚本文件,若缺失,打开NuGet包管理器控制台执行命令重新安装:Install-Package Bootstrap清理重建项目
点击Visual Studio菜单栏生成->清理解决方案,再执行生成解决方案,同时按Ctrl+Shift+Del清除浏览器缓存,避免旧缓存干扰。调整脚本引用顺序
Bootstrap 5及以上版本无需依赖jQuery(除非项目其他模块需要),可移除@Scripts.Render("~/bundles/jquery");若必须保留jQuery,确保其在Bootstrap脚本之前引用。直接引用脚本替代Bundle
若Bundle配置问题无法快速排查,可暂时跳过Bundle,直接引用Bootstrap脚本:<script src="~/Scripts/bootstrap.bundle.min.js"></script>
内容的提问来源于stack exchange,提问作者saravanan chandrasekaran
相关产品推荐
相关产品推荐

