简体   繁体   English

为什么我的脚本包会加载ASP.Net MVC中的每个页面视图?

[英]Why do my script bundles load with every page view in ASP.Net MVC?

I'm a little confused. 我有点困惑。 I have my _layout.cshtml page below and it has a bunch of bundles containing .css and .js files. 我有下面的_layout.cshtml页面,它有一堆包含.css和.js文件的包。 When I first load the site it runs through each file in the bundles. 当我第一次加载网站时,它会运行捆绑包中的每个文件。 Fine, OK, great! 好的,好的,太棒了! But then on each new view, each line of code from every file gets run through again! 但是在每个新视图中,每个文件的每行代码都会再次运行! (ex. jQuery, bootstrap, etc) I set break points to test this and they were all hit every time I loaded a new view. (例如jQuery,bootstrap等)我设置了断点来测试这个,每次加载一个新视图时都会被击中。

I thought the whole purpose was the browser would be to cache these files so they wouldn't load each time a new view gets called? 我认为整个目的是浏览器将缓存这些文件,以便每次调用新视图时都不会加载它们? Am I missing some understanding here? 我在这里错过了一些理解吗? Did I not set up the layout correctly? 我没有正确设置布局吗? Are they not being loaded but the code is being run through (that's why all my break points are being hit) with each new view? 他们没有被加载,但是每个新视图都会运行代码(这就是我的所有断点都被击中的原因)?

Is there a way to set it up so this doesn't happen and all the files are loaded once when my clients visit my home page? 有没有办法设置它所以这不会发生,所有文件都加载一次当我的客户访问我的主页? I don't understand why each .js file (ex. bootstrap) is loaded on every view that gets loaded!! 我不明白为什么每个.js文件(例如bootstrap)都被加载到每个加载的视图上!

Here is my layout page 这是我的布局页面

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">

  <title>Yogabandy - @ViewBag.Title</title>
  @Styles.Render("~/Content/yogabandy-base/css") 
  @RenderSection("content", required: false) 
  @Scripts.Render("~/bundles/modernizr")

</head>

<body>

  @Html.Partial("_Navbar")


  @RenderBody()

  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDUWFuy0zfuKe4uig_koh3O6SRDaf40gA4&libraries=places" async defer></script>
  @Scripts.Render("~/bundles/jquery") 
  @Scripts.Render("~/bundles/bootstrap") 
  @Scripts.Render("~/bundles/jqueryval") 
  @Scripts.Render("~/bundles/yogabandy-base") 
  @RenderSection("scripts", required: false)
</body>

</html>

In ASP.Net MVC calling (returning) a View (reload or change of URL assuming standard routing) is considered a new “page” load. 在ASP.Net MVC中调用(返回)视图(重新加载或更改URL,假设标准路由)被视为新的“页面”加载。

For conventional web browsing AFAIK when a page loads ie. 对于传统的网页浏览AFAIK,当页面加载即。 a typical http(s) request, the page has to load from top to bottom, including re-loading the JS and CSS. 一个典型的http(s)请求,页面必须从上到下加载,包括重新加载JS和CSS。

Because a standard http request (eg. GET) is stateless, on the client side, the browser will use exactly the HTML, CSS and JS it is told to use. 因为标准的http请求(例如GET)是无状态的,所以在客户端,浏览器将使用它被告知使用的HTML,CSS和JS。 It cannot “guess” what JS you might want or not want to load. 它无法“猜测”您可能想要或不想加载的JS。

The web browser would generally cache JS and CSS, assuming no “cache-busting” is being used. 假设没有使用“缓存破坏”,Web浏览器通常会缓存JS和CSS。

To avoid reloading of the whole page, you can use AJAX techniques, or now a single-page-application approach: https://www.asp.net/single-page-application 为了避免重新加载整个页面,您可以使用AJAX技术,或者现在采用单页面应用程序方法: https//www.asp.net/single-page-application

If you are not using SPA or Partial view/page rendering , when you navigate from one view to another view a complete post back will happen and new content will be loaded to the DOM, in that case all the resources will be requested again by the page to the server. 如果您使用SPAPartial view/page rendering ,当您从一个视图导航到另一个视图时,将发生完整的回发并且将新内容加载到DOM,在这种情况下,将再次请求所有资源。页面到服务器。 If the content like JS/Images/CSS is already cached, in that case content will not be loaded from server but from the cache, but complete JavaScript will execute again. 如果像JS/Images/CSS这样的内容已经被缓存,那么内容将不会从服务器加载而是从缓存中加载,但完整的JavaScript将再次执行。

If you don't want this to happen, go for partial page rendering or implement SPA . 如果您不希望发生这种情况,请转到部分页面呈现或实施SPA

You can read more about SPA here https://www.asp.net/single-page-application AND MSDN 您可以在https://www.asp.net/single-page-applicationMSDN上阅读有关SPA的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Viewbag无法显示在asp.net MVC视图页面中? - Why Viewbag not show in asp.net mvc view page? 如何停止在页面加载时显示部分视图 - ASP.NET MVC - How can I stop showing a partial view on page load - ASP.NET MVC 一个页面上的Asp.net MVC多个(相同)局部视图<script> tags inside Partial View - Asp.net MVC Multiple (of the same) Partial Views on a page with <script> tags inside Partial View 每次提交页面时显示加载程序MVC Asp.net - Display a loader every time a page is submitted MVC Asp.net 如果asp.net mvc中的模型不为null,如何从加载视图中调用jquery函数? - How do i call jquery function from the view on load if model is not null in asp.net mvc? ASP.NET MVC:我的事件监听器(在视图中<script> tag) only work once - Asp.net mvc: My event listeners (in the View <script> tag) only work once ASP.NET 核心 MVC 中的 javascript 捆绑包未在 IIS 上提供 - javascript bundles in ASP.NET Core MVC not being served on IIS ASP.NET MVC 4捆绑缩小确定但没有混淆 - ASP.NET MVC 4 bundles minification OK but there is NO obfuscation ASP.NET MVC脚本捆绑包加载时间太长 - ASP.NET MVC Script Bundle Takes Too Long To Load 从ASP.NET MVC3中的局部视图加载Javascript - Load Javascript from a Partial View in ASP.NET MVC3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM