VS2012 - Web窗体 - 捆绑混乱 [英] VS2012 - Web Forms - Bundling Confusion

查看:168
本文介绍了VS2012 - Web窗体 - 捆绑混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Visual Studio 2012中创建一个新的ASP.NET Web窗体项目不幸的是,默认的Site.Master文件是非常混乱。 (我张贴这些问题在一起,因为他们是非常相关的,并引用同一个code不少。)

首先,我的已经明白的在目标捆绑和微小,所以没有必要讨论。不过,我的的理解与被包含在默认的母版页中的脚本的方式去。

问1:

为什么叫捆绑〜/包/ WebFormsJs的BundleConfig.cs文件被创建,但在母版页,这些同一个人.js文件都被一个在ScriptManager的罗列出来呢?

在BundleConfig.cs:

  bundles.Add(新ScriptBundle(〜/包/ WebFormsJs)。包括(
              〜/脚本/的WebForms / WebForms.js
              〜/脚本/的WebForms / WebUIValidation.js
              〜/脚本/的WebForms / MenuStandards.js
              〜/脚本/的WebForms / Focus.js
              〜/脚本/的WebForms / GridView.js
              〜/脚本/的WebForms / DetailsView.js
              〜/脚本/的WebForms / TreeView.js
              〜/脚本/的WebForms / WebParts.js));

里面的Site.Master:

 <身体GT;
<形式=服务器>
< ASP:的ScriptManager =服务器>
    <脚本和GT;
        <% - 框架脚本 - %GT;
        < ASP:的ScriptReference NAME =MsAjaxBundle/>
        < ASP:的ScriptReference NAME =jQuery的/>
        < ASP:的ScriptReference NAME =jquery.ui.combined/>
        < ASP:的ScriptReference NAME =WebForms.js大会=System.Web程序PATH =〜/脚本/的WebForms / WebForms.js/>
        < ASP:的ScriptReference NAME =WebUIValidation.js大会=System.Web程序PATH =〜/脚本/的WebForms / WebUIValidation.js/>
        < ASP:的ScriptReference NAME =MenuStandards.js大会=System.Web程序PATH =〜/脚本/的WebForms / MenuStandards.js/>
        < ASP:的ScriptReference NAME =GridView.js大会=System.Web程序PATH =〜/脚本/的WebForms / GridView.js/>
        < ASP:的ScriptReference NAME =DetailsView.js大会=System.Web程序PATH =〜/脚本/的WebForms / DetailsView.js/>
        < ASP:的ScriptReference NAME =TreeView.js大会=System.Web程序PATH =〜/脚本/的WebForms / TreeView.js/>
        < ASP:的ScriptReference NAME =WebParts.js大会=System.Web程序PATH =〜/脚本/的WebForms / WebParts.js/>
        < ASP:的ScriptReference NAME =Focus.js大会=System.Web程序PATH =〜/脚本/的WebForms / Focus.js/>
        < ASP:的ScriptReference NAME =WebFormsBundle/>
        <% - 站点脚本 - %GT;    < /脚本>
< / ASP:ScriptManager的>

正如你可以看到每个....那些相同的.js文件中分别在上市的ScriptManager。我甚至不看到这是在任何地方创建BundleConfig.cs外的WebFormsJs捆绑的参考。为什么这个包有史以来如果这些JavaScript文件的打算是在单独的ScriptManager此处引用?

问题2:

为什么以这种方式被使用一个ScriptManager呢?我是IM pression的ScriptManager被要求微软的Ajax版本,如使用的UpdatePanel下。什么是使用的ScriptManager这里...只是简单地注册的javascript文件的目的是什么?

问题3:

在通过对比的ScriptManager在向上的的Site.Master它使用以下方法,而不是顶部注册javascript文件的区别是什么?

 <%:Scripts.Render(〜/包/ Modernizr的)%>

问题4:

使用ScriptManager内我也注意到这些:

 < ASP:的ScriptReference NAME =MsAjaxBundle/>
        < ASP:的ScriptReference NAME =jQuery的/>
        < ASP:的ScriptReference NAME =jquery.ui.combined/>

...我至少可以从BundleConfig.cs承认MsAjaxBundle,但在哪里jQuery和jquery.ui.combined界定?我做了搜索,发现packages.config给他们一个参考。

 <包ID =jQuery的版本=1.7.1.1targetFramework =net45/>
<包ID =jQuery.UI.Combined版本=1.8.20.1targetFramework =net45/>

但我不明白什么是要么会在这里。我以为packages.config用于的NuGet。另外......我甚至不看这里列出这些jQuery的.js文件的位置的路径。他们只是在这里列出奇怪地与.NET Framework(4.5在我的情况)的特定版本有关。为什么一个javascript资源将与一个版本的.NET Framework的相关超越我。

总之,问题4是这样的:被添加在ScriptManager的资源jQuery的如何/使用吗?为什么我看不到了jQuery .js文件被捆绑在一起的BundleConfig.cs像所有其他包??

问题5:

我可以删除的Site.Master如下脚本引用,如果我不使用的UpdatePanel和那些排序微软Ajax控件的规划?我有点困惑,为什么这是即使在默认情况下包括在这里。

 < ASP:的ScriptReference NAME =MsAjaxBundle/>


解决方案

更新:这是一个新的博客文章也了解更多这方面的谈判:<一href=\"http://blogs.msdn.com/b/webdev/archive/2012/09/21/asp-net-4-5-scriptmanager-improvements-in-webforms.aspx\">ASP.NET文章

基本上web表单+捆绑看起来像这样因一堆旧行为,我们无法在ScriptManager的改变。

在问候你的具体问题:


  1. 基本上,这是如此的重复数据删除工作正常,脚本管理器具有哪些$ P $被scriptmapped pvents他们的原始脚本资源的限制,所以他们需要映射到磁盘,然后才能正确重复数据删除,因为这些文件已经包含在捆绑。
    该WebformsBundleJs是使用ScriptManager nupkgs内的preAppStart code内部创建一个脚本映射。 (我同意这是几乎不可能发现)


  2. 4.5的新功能,如不显眼的审定要求的jQuery(通过ScriptManager的),这就是为什么使用脚本管理,以确保jQuery的不会被渲染两次。


  3. 这将正常工作,但它永远不会与ScriptManager的重复数据删除。所以对于Modernizr的不会是一个问题。


  4. jQuery的包砸在你的脚本文件夹中的文件的jquery到磁盘。


  5. 这是参考其中包含了所有的AJAX脚本的msajaxbundle拉,如果你并不需要/想他们,我认为其安全地删除。


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.

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?

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

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>

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?

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?

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") %>

Question 4:
Inside the ScriptManager I also noticed these:

        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="jquery" />
        <asp:ScriptReference Name="jquery.ui.combined" />

... 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" />

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.

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??

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" />

解决方案

UPDATE: This is a new blog post which also talks about this more: ASP.NET article

Basically webforms + bundling looks like this due to a bunch of legacy behavior that we weren't able to change in scriptmanager.

In regards to your specific questions:

  1. 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)

  2. 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.

  3. This will work fine, but it will never dedupe with ScriptManager. So for modernizr it won't be an issue.

  4. The jquery packages drop the jquery files to disk in your Scripts folder.

  5. 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窗体 - 捆绑混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆