7

I am working on this ASP.NET MVC project. I did not do any changes to the _Layout.cshtml or BundleConfig.cs or Web.config. When I ran the project, it is showing the below exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.' [External Code] ASP._Page_Views_Shared__Layout_cshtml.Execute() in _Layout.cshtml

I tried the minification method but still it failed. If it is possible to view, please check the photos below.

Can somebody help me resolve this issue, please?

Picture01

Picture02

Pciture03

Dale K
  • 25,246
  • 15
  • 42
  • 71
oybek
  • 79
  • 1
  • 2

4 Answers4

11

Replace the following:

bundles.Add(new ScriptBundle("~/bundles/mybundle").Include(
            "~/Scripts/...",
            "~/Scripts/..."));

with this:

bundles.Add(new Bundle("~/bundles/mybundle").Include(
            "~/Scripts/...",
            "~/Scripts/..."));
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Akanksha
  • 111
  • 1
  • 2
3

In the method BundleConfig.RegisterBundles I replaced all existing instantiation of ScriptBundle to instances of Bundle and it worked as expected.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
LJILJANA
  • 39
  • 1
  • 1
  • This prevents any of your scripts from being minified....my understanding anyway. So make sure you are including minified versions. – Post Impatica Jun 22 '22 at 19:46
  • Since I only had the problem with the bootstrap bundle, I only updated it for bootstrap, and it worked. Thank you very much. I was about to start pulling out my hair. – Nel Prinsloo Jan 24 '23 at 22:40
2

Same issue here with an older Web API project after upgrading all NuGet packages, specifically to Bootstrap 5.1

The same happens on a new default Web API project in VS 2019 after upgrading its packages to Bootstrap 5.1 - NullReferenceException in _Layout.cshtml!

Apparently dependencies for Bootstrap 5.1 are not yet handled propery by ASP.NET projects -
Downgrading (via VS Nuget Manager) to the last available 4.x version solved the issue for my projects.

Jay
  • 6,572
  • 3
  • 37
  • 65
1

The suggested solution provided by Akanksha resolved the originally described problem after I upgraded an older ASP.NET WebAPI project from Bootstrap 3.x to 5.1.x

In the method BundleConfig.RegisterBundles I replaced all existing instantiation of ScriptBundle to instances of Bundle and it worked as expected.

bundles.Add(new Bundle("~/bundles/bootstrap").Include(
    "~/Scripts/bootstrap.js",
    "~/Scripts/respond.js"));
Dale K
  • 25,246
  • 15
  • 42
  • 71
JoKi
  • 37
  • 3
  • So why add a new answer that is identical to an existing one? What values does this add? – Dale K Feb 24 '23 at 03:47
  • Confirmation. What's your beef? Ljiljana answered the same after me and you're not keen to comment? Commenting after 1.5 years, feeling bored? – JoKi Jun 09 '23 at 17:18