I am trying to figure out how this works. In a site master from VS template (Web Forms), I read the following:
<asp:ScriptManager runat="server">
<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>
Now some of the scripts have just names, others have names and paths + assembly. How does the application figure out the paths of those only with names and why is there this difference with the others. Also what is Assembly needed for?
If you expand the References node of your web project in the Solution Explorer or you open the Manage NuGet Packages tab and filter the installed packages with "AspNet.ScriptManager." you'll see references such as AspNet.ScriptManager.jQuery
and Microsoft.AspNet.ScriptManager.MSAjax
. Those packages ship with the default VS template and their job is to add ScriptMappings in the PreApplicationStart method of the application. For example, the description of the AspNet.ScriptManager.jQuery
package reads as follows:
This package contains the AspNet.ScriptManager.jQuery assembly that will automatically register jQuery x.y.z with the ScriptManager as "jquery".
So, those are the names used by the ScriptManager.
And here is a sample of the ScriptMapping definitions added by those packages:
string str = "x.y.z";
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"
});
In ASP.NET 4.5 the Microsoft Ajax script files (MicrosoftAjaxCore etc) and the WebForms scripts (GridView.js etc) were decoupled so they can be served from your application Scripts folder rather than being loaded up from System.Web
. The reason both the Assembly and the Path attributes are present is because ScriptManager special cases these scripts when it tries to load them, so special arrangements with ScriptManager code had to be done to make this work. Basically when the ScriptManager tries to load up these scripts, it can load them up from System.Web or the path attribute and eventually in this case, the ScriptManager dedupes the script references and serves the scripts from the path attribute.