Script Manager in ASP.NET 4.5 Web Forms - what does it do? How do you use Bundling & Minification with it?

StackOverflow https://stackoverflow.com/questions/20822079

  •  22-09-2022
  •  | 
  •  

Pergunta

I have created a new ASP.NET 4.5.1 web forms project.

The master page has a script manager in it - and it lists a large number of scripts, including a reference to jquery and bootstrap:

<asp:ScriptManager runat="server">
    <Scripts>
        <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
        <%--Framework Scripts--%>
        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="jquery" />
        <asp:ScriptReference Name="bootstrap" />
        <asp:ScriptReference Name="respond" />
        <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>

These scripts are then included in the page.

I always thought script manager was just for AJAX related scripts, but it is now including seemingly all js scripts. It also seems to conflict with Bundling & Minification - as it is including scripts rather than bundle references.

I have searched Google but an unable to find what script manager actually is beyond its relation to AJAX.

UPDATE

I found this reference to the scripts property of the script manager, though it doesn't explain the benefit /reason for listing all page scripts in it: http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.scripts(v=vs.110).aspx

Foi útil?

Solução

These two features do indeed work together, as bundling scripts and managing scripts are two separate tasks. Bundling is obviously the simpler of the two, as it just refers to the technique of combining multiple script files into one in order to increase performance by decreasing the number of requests to the server that are necessary. But there are perfectly valid reasons to have the ScriptManager deal with your bundled JavaScript files (and the scripts that compose them) as well as your non-bundled ones. For instance, you can use the ScriptManager to switch between loading non-minified scripts while in debug mode and minified scripts while in release mode. You can also specify a LoadSuccessExpression, which will be used to check that the script was loaded correctly, and if it wasn't it can be loaded from a CDN based on the CdnPath property. These would be specified in a ScriptResourceDefinition, possibly in App_Code/BundleConfig.cs or in Global.asax, but you still want to add the ScriptReference elements to the ScriptManager.

Outras dicas

Optimization 1.1.0.0 does not remove duplicate scripts registered in the ScriptManager.

There is a bug in Microsoft.AspNet.Web.Optimization component version 1.1.0.0 (stable) (version 1.0.0.0 doesn’t have this bug. The difference is in the behavior of the method GetBundleContents of BundleResolver object. If I call this method in version 1.0.0.0 and 1.1.0.0 I get different results. Version 1.1.0.0 returns incorrect path to the script without the ~ character and therefore this path does not match the path specified in ScriptReference Path.

http://aspnetoptimization.codeplex.com/workitem/94

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top