Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 734
niico Avatar asked Oct 20 '22 18:10

niico


1 Answers

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.

like image 98
regularmike Avatar answered Oct 23 '22 23:10

regularmike