5

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

niico
  • 11,206
  • 23
  • 78
  • 161
  • ASP.NET WebForms predates modern-day bundling and AJAX. ScriptManager was used originally to ensure scripts necessary for older WebForms parts, such as the Calendar control and WebParts were rendered in the page. Note that generally-speaking, WebForms is incompatible with modern-day web development, for that use ASP.NET MVC which gives you more control over generated markup. – Dai Dec 29 '13 at 04:27
  • I have used MVC before - though I just need to get something up fast without all the complexity of MVC. Web forms would seem to be far better at that than MVC. – niico Dec 29 '13 at 04:48
  • The scriptmanager has been updated in asp.net 4.5 - and bundling & minification has been added to web forms. These are up to date features - and should work together no? – niico Dec 29 '13 at 04:49

2 Answers2

3

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.

regularmike
  • 1,167
  • 10
  • 22
  • Thanks. Bundling + Minification automatically minifies only when not in debug mode too. It doesn't seem that useful? – niico Jan 17 '14 at 07:40
  • But can you do the CDN stuff with bundling and minification only, like CdnPath, CdnDebugPath, and CdnSupportsSecureConnection? Anyway, I'm not saying it's useful, just that it does have some features that some developers might want to use. I think the real answer is that they don't like throwing anything away, so they'll try to improve it instead, even when there may be a newer, simpler/cleaner way to do it. Here's a blog post about ScriptManager improvements in ASP.NET 4.5: http://go.microsoft.com/fwlink/?LinkID=301884 – regularmike Jan 17 '14 at 14:50
  • 1
    thanks - so it's not worth using it unless there's something specific that requires it - like AJAX - I guess?! – niico Jan 18 '14 at 06:31
  • Yeah, or unless you want finer control over when scripts get added or removed, and their sources. – regularmike Jan 22 '14 at 18:08
0

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

Cyrus
  • 2,261
  • 2
  • 22
  • 37