u936293
u936293

Reputation: 16224

ToolkitScriptManager: how are some script files located?

I have a ASP.NET 4.5 Webform project using the standard Internet template. Included in Site.Master is:

<ajaxToolkit:ToolkitScriptManager runat="server">
    <Scripts>
        <%--Framework Scripts--%>
        <asp:ScriptReference Name="jquery" />   <!-- ??? -->
        <asp:ScriptReference Name="jquery.ui.combined" />  <!-- ?? -->
        <asp:ScriptReference Name="WebForms.js" Path="~/Scripts/WebForms/WebForms.js" />
        ...

The two jquery lines above apparently cause the following to be generated in the html document:

<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.20.js" type="text/javascript"></script>

May I know how/where does ASP.NET know the link to these two files?

--

July 30 updates:

I found this which describes how the jQuery scripts are registered in a 4.5 Web Form application. The code sample shows exactly how the selected version of the jQuery scripts are linked in. It says it is executed in the PreApplicationStart method which runs before Application_Start. I searched my whole project for "PreApplicationStart" but could not find anything. Anyone knows where this is located?

Upvotes: 2

Views: 1369

Answers (1)

Maxim Kornilov
Maxim Kornilov

Reputation: 6055

From .Net framework 4.0 ScriptManager support script mapping feature. So, you can register (associate) any name (even for your own scripts) with scripts paths. The main benefits is that you can specify scripts to be used for debug, release configuration. You also have ability to specify CDN location of scripts. ScriptManager will choose correct one for current configuration during runtime.

Basically script mapping is registered in application start as in the following example:

void Application_Start(object sender, EventArgs e) {
    // map a simple name to a path
    ScriptManager.ScriptResourceMapping.AddDefinition("jQuery", new ScriptResourceDefinition {
        Path = "~/scripts/jquery-1.7.1.min.js",
        DebugPath = "~/scripts/jquery-1.7.1.js",
        CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.1.min.js",
        CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.7.1.js"
    });
}

Edit: In ASP.NET 4, were added the concept of a PreApplicationStart method that an assembly can use to execute code early on in the appdomain without any configuration. So, registration mapping is added by AspNet.ScriptManager.jQuery and AspNet.ScriptManager.jQueryUI libraries which is added by default in template via nuget.

Basically this library consist from one class as in the following example:

[EditorBrowsable(EditorBrowsableState.Never)]
public static class PreApplicationStartCode
{
  public static void Start()
  {
     string str = "2.0.3";
     ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition()
     {
       Path = "~/Scripts/jquery-" + str + ".min.js",
       DebugPath = "~/Scripts/jquery-" + str + ".js",
       CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + str + ".min.js",
       CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-" + str + ".js",
       CdnSupportsSecureConnection = true,
       LoadSuccessExpression = "window.jQuery"
     });
  }
}

Upvotes: 1

Related Questions