问题描述
我通过 Visual Studio 2012 创建了一个新的 ASP.NET Web 窗体项目.不幸的是,默认的 Site.Master 文件非常混乱.(我将这些问题放在一起是因为它们非常相关并且引用了很多相同的代码.)
I created a new ASP.NET Web Forms Project through Visual Studio 2012. Unfortunately, the default Site.Master file is very confusing. (I am posting these questions together because they are very related and reference the same code quite a bit.)
首先,我已经了解捆绑和缩小的目的,因此无需讨论.但是我不理解脚本被包含在默认母版页中的方式.
First off, I already understand the purpose of bundling and minification, so no need to discuss that. However I do not understand what is going with the way the scripts are being included in the default master page.
问题 1:
为什么在 BundleConfig.cs 文件中创建了一个名为~/bundles/WebFormsJs"的包,而在母版页中,每个相同的单独 .js 文件都在 ScriptManager 中一一列出?
Question 1:
Why is a bundle called "~/bundles/WebFormsJs" being created in the BundleConfig.cs file, and yet in the master page, each of these same individual .js files are being listed out one by one in the ScriptManager?
BundleConfig.cs 内部:
Inside BundleConfig.cs:
bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
"~/Scripts/WebForms/WebForms.js",
"~/Scripts/WebForms/WebUIValidation.js",
"~/Scripts/WebForms/MenuStandards.js",
"~/Scripts/WebForms/Focus.js",
"~/Scripts/WebForms/GridView.js",
"~/Scripts/WebForms/DetailsView.js",
"~/Scripts/WebForms/TreeView.js",
"~/Scripts/WebForms/WebParts.js"));
Site.Master 内部:
Inside Site.Master:
<body>
<form runat="server">
<asp:ScriptManager runat="server">
<Scripts>
<%--Framework Scripts--%>
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
如您所见……每个相同的 .js 文件都单独列在 ScriptManager 中.我什至没有看到对在 BundleConfig.cs 之外的任何地方创建的WebFormsJs"包的引用.如果要在 ScriptManager 中单独引用这些 javascript 文件中的每一个,为什么还要创建该包?
As you can see.... each of those same .js files are individually listed in the ScriptManager. I don't even see a reference to the "WebFormsJs" bundle that was created anywhere outside of BundleConfig.cs. Why was that bundle ever created if each of these javascript files were going to be referenced individually here in the ScriptManager?
问题 2:
为什么以这种方式使用 ScriptManager?我的印象是 ScriptManager 是 Microsoft 版本的 Ajax 所必需的,例如使用 UpdatePanels.在这里使用 ScriptManager 的目的是什么......只是为了简单地注册 javascript 文件?
Question 2:
Why is a ScriptManager being used in this way at all? I was under the impression ScriptManager was required for Microsoft's version of Ajax such as using UpdatePanels. What is the purpose of using ScriptManager here... just to simply register javascript files?
问题 3:
通过 ScriptManager 注册 javascript 文件与使用以下方法在 Site.Master 顶部注册有什么区别?
Question 3:
What is the difference in registering javascript files through the ScriptManager versus up at the top of Site.Master where it uses the following approach instead?
<%: Scripts.Render("~/bundles/modernizr") %>
问题 4:
在 ScriptManager 中,我还注意到这些:
Question 4:
Inside the ScriptManager I also noticed these:
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="jquery.ui.combined" />
...我至少可以从 BundleConfig.cs 中识别出MsAjaxBundle",但是 jquery 和 jquery.ui.combined 在哪里定义的?我进行了搜索,并在 packages.config 中找到了对它们的引用.
... I can at least recognize "MsAjaxBundle" from BundleConfig.cs, but where are jquery and jquery.ui.combined defined? I did a search and found a reference to them in packages.config.
<package id="jQuery" version="1.7.1.1" targetFramework="net45" />
<package id="jQuery.UI.Combined" version="1.8.20.1" targetFramework="net45" />
但我也不明白这里发生了什么.我认为packages.config 用于NuGet.另外...我什至没有看到此处列出的这些 jQuery .js 文件位置的路径.它们只是在此处列出,并且奇怪地与特定版本的 .NET Framework(在我的情况下为 4.5)相关联.我不明白为什么 javascript 资源会与 .NET Framework 的某个版本相关联.
But I don't understand what is going on here either. I thought packages.config was used for NuGet. Plus... I don't even see a path listed here for the location of these jQuery .js files. They are just listed here and strangely associated with a particular version of the .NET Framework (4.5 in my case). Why a javascript resource would be associated with a version of the .NET Framework is beyond me.
无论如何,问题 4 是这样的:ScriptManager 中的资源jquery"是如何添加/使用的?为什么我没有看到 jQuery .js 文件像所有其他包一样被捆绑在 BundleConfig.cs 中?
Anyway, question 4 is this: How is the resource "jquery" in the ScriptManager being added/used? Why don't I see the jQuery .js files being bundled up together in BundleConfig.cs like all the other bundles??
问题 5:
如果我不打算使用 UpdatePanel 和那些 Microsoft Ajax 控件,我可以从 Site.Master 中删除以下脚本引用吗?我有点困惑为什么默认情况下甚至将其包含在此处.
Question 5:
Can I remove the following script reference from Site.Master if I am not planning on using UpdatePanel and those sort of Microsoft Ajax controls? I am a little confused as to why this is even included here by default.
<asp:ScriptReference Name="MsAjaxBundle" />
推荐答案
更新: 这是一篇新的博客文章,也更多地讨论了这个问题:ASP.NET 文章
UPDATE: This is a new blog post which also talks about this more: ASP.NET article
由于我们无法在脚本管理器中更改的一系列遗留行为,基本上 webforms + 捆绑看起来像这样.
Basically webforms + bundling looks like this due to a bunch of legacy behavior that we weren't able to change in scriptmanager.
关于您的具体问题:
基本上这是重复数据删除工作正常,脚本管理器对原始脚本资源有限制,这会阻止它们被脚本映射,因此它们需要映射到磁盘,然后正确删除重复数据,因为文件已经包含在内在捆绑.WebformsBundleJs 是在 ScriptManager nupkgs 内的 PreAppStart 代码内创建的脚本映射.(我同意这几乎不可能被发现)
Basically this is so deduping works correctly, script manager has a limitation for the origional script resources which prevents them from being scriptmapped, so they need to be mapped to disk which then gets properly deduped because the files are already included in the bundle. The WebformsBundleJs is a script mapping that is created inside of the PreAppStart code inside of the ScriptManager nupkgs. (I agree this is nearly impossible to discover)
新的 4.5 功能,如不显眼的验证需要 jquery(通过脚本管理器),这就是为什么使用脚本管理器来确保 jquery 不会被渲染两次.
New 4.5 features like unobtrusive validation required jquery(via scriptmanager), which is why script manager was used to ensure jquery doesn't get rendered out twice.
这会正常工作,但它永远不会使用 ScriptManager 进行重复数据删除.所以对于 Modernizr 来说,这不是问题.
This will work fine, but it will never dedupe with ScriptManager. So for modernizr it won't be an issue.
jquery 包将 jquery 文件放到 Scripts 文件夹中的磁盘中.
The jquery packages drop the jquery files to disk in your Scripts folder.
该引用引入了包含所有 ajax 脚本的 msajaxbundle,如果您不需要/想要它们,我认为可以安全删除.
That reference pulls in the msajaxbundle which contains all of the ajax scripts, if you don't need/want them, I think its safe to remove.
这篇关于VS2012 - Web 表单 - 捆绑混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!