Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Inside scripts folder's bootstrap.js not happening include on bundle. Showing error message inside layout page while runtime.

This is the code inside BundleConfig page:

C#
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"));


This is the code inside layout page:

@Scripts.Render("~/bundles/bootstrap")


error showing:
System.NullReferenceException: 'Object reference not set to an instance of an object.'


Please find below the link of error page image

error image

What I have tried:

I changed the bundles/bootstrap to bundles/Scripts then project run without any error.

C#
bundles.Add(new ScriptBundle("~/bundles/scripts")
                .Include("~/Scripts/jquery-{version}.js")
                .Include("~/Scripts/jquery.validate*")
                .Include("~/Scripts/bootstrap.js")
        );

    // 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 StyleBundle("~/bundles/css").Include(
                "~/Content/bootstrap.css",
                "~/Content/site.css"));


But the MVC header menu design get broken.

How can I solve the problem?
Posted
Updated 24-Nov-21 10:14am
v4
Comments
Nono Calero 15-Jan-22 6:59am    
I don't know why the boostrap team are include the @ in the javascript code, that is having conflict in the razor pages.

Quote:
Razor
@Scripts.Render("~/bundles/bootstrap")
Look at your BundleConfig.cs file - the bundles you have registered are:
  • "~/bundles/jquery"
  • "~/bundles/jqueryval"
  • "~/bundles/modernizr"
  • "~/bundles/Scripts"
  • "~/Content/css"

There is no bundle registered for ~/bundles/bootstrap.

Either change the bundle path to match the Render call, or change the Render call to match the registered bundle path.
 
Share this answer
 
Comments
Member 12955507 13-Jul-21 5:22am    
I have added bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js")); inside BundleConfig.cs but still the error raising
Richard Deeming 13-Jul-21 5:27am    
Did you recompile after changing the bundles file?

Does the bootstrap.js file exist in the ~/scripts/ folder?
Member 12955507 13-Jul-21 5:56am    
Yes dear. It is there. I don't know what is the reason behind this. I googled like " @Scripts.Render("~/bundles/bootstrap") showing Object reference not set to an instance of an object" But didn't get any result related to my problem.
Richard Deeming 13-Jul-21 5:58am    
If you click on the "View details" link, what's the full stack trace of the exception?

It's possible that the bundler & minifier package is struggling to minify the Bootstrap JS file.
Member 12955507 13-Jul-21 6:08am    
Here is the view details page kindly check it error image
After updating to Bootstrap 5,
.Include("~/Scripts/bootstrap.js") this line is not allowed.
Remove this line and it works fine.
 
Share this answer
 
I don't know why the boostrap team are include the @ in the javascript code, that is having conflict in the razor pages.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900