简体   繁体   中英

Added to layout page scripts dont work in ASP.NET MVC 5

While making my ASP.NET project I've faced a problem that script's, which were wrote in "Head" section in my layout page, are not available to use in rendered view. Content of layout page looks like this:

 <!DOCTYPE html> <html> <head> <link href="/Content/bootstrap.css" rel="stylesheet"/> <script src="/Scripts/bootstrap.js"></script> <script src="/Scripts/jquery-1.9.1.js"></script> <title>@ViewBag.Title</title> </head> <body> <div class="container-fluid"> @RenderBody() </div> </body> </html> 

Inner page contains Bootstrap's Carousel, which is not working if I havent put the "script" strings directly into inner page. What am I missing? Why cant i add scripts into layout?

尽管事实上布局和正文已合并到单个页面中,但是ASP.NET仍然认识到布局文件是文件系统中更深的一个节点,并相应地生成路径。因此,GSerg是正确的: 添加到布局页面脚本在ASP中不起作用.NET MVC 5

Not true. They just must be loaded at the bottom of the page just above the closing "body" tag in most cases. The divs, ids, etc that you are trying to manipulate will not have been generated at the time those js scripts run.

If you're using MVC 5, bundling is a much easier way to handle both your CSS and js, IMHO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM