【问题标题】:ASP.NET Receiving NullReferenceException on jQuery BundleASP.NET 在 jQuery Bundle 上接收 NullReferenceException
【发布时间】:2020-08-15 02:19:48
【问题描述】:

问题:我在 @Scripts.Render("~bundles/jquery") 上收到 NullReferenceException

我昨晚部署并进行了测试,一切都很好。今天早上醒来,决定检查网站并注意到它。我也没有碰过任何奇怪的东西。

这不会在本地发生!!!!!!

代码片段

@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

BundleConfig

public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));
    }

我尝试过的事情:

  • 重建 + 完全部署
  • 重置/回收
  • 使用硬编码版本重命名捆绑包 有什么建议吗?这是与 Minify 相关的吗?

编辑#1

编辑#2 我可以确认在访问 https://example.com/Scripts/jquery-3.4.1.min.js 时看到正在加载 JS 文件

编辑#3 我尝试替换所有捆绑引用以从 Scripts & Content 文件夹中提取,现在我遇到了一些奇怪的 jQuery 错误...注意,我更喜欢使用捆绑。

<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/bootstrap-theme.min.css" rel="stylesheet" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/modernizr-2.8.3.js"></script>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

编辑#4 我有一个使用最新 CDN 的 Bootstrap 解决方案,但我更喜欢使用标准捆绑。

【问题讨论】:

  • 您可以尝试在本地调试您的捆绑包,就像在生产环境中一样,将这一行放在您的 RegisterBundles 中:BundleTable.EnableOptimizations = true;我怀疑你的本地正在工作,因为它实际上并没有在调试时捆绑
  • 真正的问题是,为什么会突然出现捆绑问题?它在生产/发布中按预期工作。是否有任何与捆绑相关的 IIS 配置设置可能导致它?
  • 我刚刚启用了优化,现在它发生在本地。 System.NullReferenceException: 'Object reference not set to an instance of an object.' Microsoft.Ajax.Utilities.FunctionObject.SafeIsReferenced(Syste[..]
  • 错误堆栈跟踪是否显示任何以任何布局视图或资源文件结尾的行号?
  • 它在 OP 中基本上是一样的。 @Scripts.Render("~/bundles/jquery")

标签: javascript asp.net asp.net-mvc


【解决方案1】:

尝试拼出 jquery 版本而不是输入 {version}

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery.1.6.22.js"));

    bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*"));

    // Use the development version of Modernizr to develop with and learn from. Then, when you're
    // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
    bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                "~/Scripts/modernizr-*"));

    bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
              "~/Scripts/bootstrap.js"));

    bundles.Add(new StyleBundle("~/Content/css").Include(
              "~/Content/bootstrap.css",
              "~/Content/site.css"));
}

并确保js文件存在于编译中

【讨论】:

  • 我会试一试,让你知道!
  • 我改了,还在收到。
  • - 是服务器上的文件吗? - 如果您访问 url yoursite.com/bundles/jquery,您是否看到正确的捆绑包?
  • 添加了截图
  • 另外,我可以确认我看到了 jQuery 文件 example.org/Scripts/jquery-3.4.1.min.js
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 2017-03-15
  • 2013-03-21
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多