使用的ScriptManager / ToolkitManager AjaxControlToolkit中加载脚本从微软的CDN [英] Loading AjaxControlToolkit Scripts from Microsoft's CDN using ScriptManager/ToolkitManager

查看:152
本文介绍了使用的ScriptManager / ToolkitManager AjaxControlToolkit中加载脚本从微软的CDN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个问题问同样的事情,但它并没有得到任何关注,现在月:<一href="http://stackoverflow.com/questions/3786088/how-to-force-ajax-control-toolkit-scripts-loading-from-cdn">How给力的Ajax控件工具包脚本从CDN加载?

我升级我的网站.NET4,而我现在使用ScriptManager的EnableCDN =真的标签。我的阿贾克斯脚本正在从微软CDN引用的我是多么的预期,但我似乎无法让我的脚本AjaxControlToolkit中从CDN加载。相反,他们所有的负载本地通过的ScriptResource.axd。

我知道那里的CDN文件的位置,我知道,我可以引用我每次使用控制时间这些文件,但我已经得到了很多传统的code加载自己的ScriptManager的,只需拔的ScriptResource.axd文件。

任何建议如何获得控制工具包脚本从CDN加载?我已经有标准WebForms.js等。

让我知道如果有什么我可以明确起来,这里是我使用的脚本经理:(我曾尝试toolkitscriptmanager)

 &LT; ASP:ScriptManager的ID =ScriptManager1=服务器的EnablePageMethods =真
EnableCdn =真EnableScriptLocalization =假
LoadScriptsBeforeUI =假的EnableViewState =假/&GT;
 

解决方案

如果一个程序集没有定义在其WebResourceAttributes一个CDN路径,EnableCDN不知道哪里去了。因此,有这一点。

不过,您可以定义自己的CDN路径,如果你知道一个CDN,有他们。我真的不知道最新的行为是否是在CDN与否。如果你正在使用的版本是40412,那么是的,它是: <一href="http://www.asp.net/ajaxlibrary/CDNACT40412.ashx">http://www.asp.net/ajaxlibrary/CDNACT40412.ashx

您定义自己的路的方式在我的博客条目(在另一个答案也提到)描述:
<一href="http://weblogs.asp.net/infinitiesloop/archive/2009/11/23/asp-net-4-0-scriptmanager-improvements.aspx">http://weblogs.asp.net/infinitiesloop/archive/2009/11/23/asp-net-4-0-scriptmanager-improvements.aspx

下面是一个例子,说明如下:

 无效的Application_Start(对象发件人,EventArgs的){
    ScriptManager.ScriptResourceMapping.AddDefinition(Foo.js的typeof(FooControl).Assembly,新ScriptResourceDefinition {
        资源名称=Foo.js
        ResourceAssembly = typeof运算(FooControl).Assembly,
        CdnPath =HTTP://yadda-yadda/foo.js
        CdnDebugPath =HTTP://yadda-yadda/foo.debug.js
    });
}
 

下面是发生了什么。首先,存在一个组件,可以称它为Foo.dll。它包含了脚本命名为Foo.js(在扩展名为.js可选)嵌入的资源。因此,ASP:ScriptReferences(在页面上或从服务器控件到来),以该资源会去的ScriptResource.axd。大会WebResourceAttribute为脚本没有定义CDN路径,所以它仍然去那里,如果你打开​​CdnMode。

您想解决这个问题,所以你要做的就是目标的脚本。要做到这一点,你必须参考它的装配和名字。他们一起识别脚本(它的主键,如果你愿意)。第1两个参数AddDefinition实现这一目标。

现在,您针对性它,你就会得到彻底重新定义它的意思是该脚本。你把它放在另一个组件,另一条路径,定义CDN路径..任何事情。

定义的资源名称和ResourceAssembly属性用于建立脚本真的还属于同一个组件它是从哪里来的。这似乎没有必要,如果你打算从CDN加载反正。但它做了两件事:(1)如果关闭CDN它仍然可以工作,和(2)它仍然能够找出如果一个调试版本的资源(foo.debug.js)或任何本地化的版本(如foo.fr-FR.js)存在于组装,从而知道CDN是否将包含版本(因为它不能很好地检查从服务器)。所以这是一个好主意,让定义的所有者(请注意,它不会违约原来的主人,如果你不指定)。

在CdnPath和CdnDebugPath性是显而易见的,在这里,你的最终目标。 EnableCdn现在应该努力!

我鼓励你使用ScriptMapping时,可以定义有关脚本的详细信息,然后把它们的名字,而不是路径。保持标记简单,它更干,因为你定义在一个地方的所有细节的脚本。

现在...你怎么做,与AjaxControlToolkit中?

您将不得不写这些映射每工具包中每个脚本之一。那不是会很有趣,也有几十个几十人。不要写那些单独为每个版本和调试脚本,虽然的错误。两者是一对,并与1映射的定义。也有一些他们的本地化版本,但我不会与该打扰 - 的消息是错误消息意味着开发人员,这实际上并不需要无论如何本地化,因为该功能是为了本地化最终内容用户。

有一件事情 - 尝试一下在1或2的ACT脚本,以确保你得到它的权利。此外,有一个更大的复杂性在这里我没有提到。

还记得我说过的名称和议会来共同确定一个脚本(它的主键)?这是真的,但有对于来自System.Web.Extensions程序或所谓的AjaxFrameworkAssembly脚本一款专业规则。在这种情况下,指定空组件,或使用如该参数全部丢失的过载,是等价的。什么是AjaxFrameworkAssembly?那么,System.Web.Extensions程序包含一个鲜为人知的功能,它允许一个组件通过汇编级属性宣称自己是AjaxFrameworkAssembly。这样做的意思是我是一个包含MS AJAX脚本组件,你应该来System.Web.Extensions程序给我,而不是。该法案以升级嵌入在System.Web.Extensions程序脚本使用此。它有自己的那些有更高版本号播音稿。所有你需要做的是引用的程序集,和preSTO,该脚本将自动重定向到从那里来代替。这里的关键是,你也许能够逃脱不指定组件在所有这些映射。但是,试一下,不要把我的话。

这一直是InfinitiesLoop答案。我现在返回到你的定期节目:)

I know there is another question asking the same thing, but it hasn't gotten any attention for months now: How to force Ajax Control Toolkit scripts loading from CDN?

I've upgraded my website to .NET4, and I'm now using the scriptManager's EnableCDN="true" tag. My Ajax scripts are being referenced from the Microsoft CDN just how I expected, but I can't seem to get my AjaxControlToolkit scripts to load from the CDN. Instead they all load locally through ScriptResource.axd.

I know where the CDN files are located, and I know that I could reference those files every time I use a control, but I've got a lot of legacy code that loads on its own from the scriptmanager, just pulling ScriptResource.axd files.

Any suggestions how to get the control toolkit scripts to load from the CDN? I already have the standard WebForms.js, etc.

Let me know if there is anything I can clear up, here is the script manager that I'm using: (I have tried the toolkitscriptmanager)

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" 
EnableCdn="true" EnableScriptLocalization="false" 
LoadScriptsBeforeUI="false" EnableViewState="false" />

解决方案

If an assembly does not define a CDN path in its WebResourceAttributes, EnableCDN won't know where to go. So there's that.

However, you can define your own CDN path if you know of a CDN that has them. I honestly don't know whether the latest ACT is on the CDN or not. If the version you are using is 40412, then yes, it is: http://www.asp.net/ajaxlibrary/CDNACT40412.ashx

The way you define your own path is described in my blog entry (also mentioned in another answer):
http://weblogs.asp.net/infinitiesloop/archive/2009/11/23/asp-net-4-0-scriptmanager-improvements.aspx

Here's an example, explained below:

void Application_Start(object sender, EventArgs e) {
    ScriptManager.ScriptResourceMapping.AddDefinition("Foo.js", typeof(FooControl).Assembly, new ScriptResourceDefinition {
        ResourceName = "Foo.js",
        ResourceAssembly = typeof(FooControl).Assembly,
        CdnPath = "http://yadda-yadda/foo.js",
        CdnDebugPath = "http://yadda-yadda/foo.debug.js",
    });
}

Here is what is happening. First of all, there exists an assembly, lets call it Foo.dll. It contains a script as an embedded resource named "Foo.js" (the .js extension is optional). So, asp:ScriptReferences (on the page or coming from a server control) to that resource will go to ScriptResource.axd. The assembly's WebResourceAttribute for the script does not define a CDN path, so it still goes there if you turn on CdnMode.

You want to fix that, so what you do is target that script. To do so, you have to refer to its assembly and name. Together, they identify the script (it's "primary key", if you will). The 1st two parameters to AddDefinition accomplish that.

Now that you targetted it, you get to completely redefine what it means to be that script. You put it in another assembly, another path, define the cdn path.. anything.

The ResourceName and ResourceAssembly properties of the definition establish that the script really still belongs to the same assembly it came from. That might seem unnecessary if you are going to load from CDN anyway. But it does two things: (1) It will still work if you turn off CDN, and (2) it will still be able to figure out if a debug version of the resource (foo.debug.js) or any LOCALIZED versions (e.g. foo.fr-FR.js) exist in the assembly, and thereby know whether the CDN will contain that version (since it can't very well check that from the server). So it's a good idea to give the definition an owner (note that it will NOT default to the original owner if you don't specify them).

The CdnPath and CdnDebugPath properties are obvious, and your ultimate goal here. EnableCdn should now work!

I encourage you to use ScriptMapping when possible to define the details about a script and then refer to them by name instead of path. Keeps the markup simple and it's more DRY since you get to define all the details for the script in one place.

NOW... How do you do that with the AjaxControlToolkit?

You're going to have to write one of these mappings for each and every script in the toolkit. That is not going to be fun, there are dozens and dozens of them. Don't make the mistake of writing separate ones for each release and debug script though. The two are a pair and are defined with 1 mapping. There are also localized versions of some of them, but I wouldn't bother with that -- the messages are error messages meant for developers, which really don't need to be localized anyway, because that feature is meant to localize content for end users.

One thing -- try it out on 1 or 2 of the ACT scripts to make sure you got it right. Also, there's one more complexity here I didn't mention.

Remember when I said the Name and Assembly come together to identify a script (its "primary key")? That's true, but there's one special rule for scripts that come from System.Web.Extensions OR the so-called "AjaxFrameworkAssembly". In that case, specifying a null assembly, or using the overload where that parameter is missing altogether, is equivalent. What is a "AjaxFrameworkAssembly"? Well, System.Web.Extensions contains a little-known feature that allows an assembly to assert itself as the "AjaxFrameworkAssembly" via an assembly level attribute. Doing that means "I am the assembly that contains ms ajax scripts, you should come to me instead of System.Web.Extensions". The ACT uses this in order to 'upgrade' the scripts that are embedded in System.Web.Extensions. It has its own copies of those scripts that have a later version number. All you have to do is reference the assembly, and presto, the scripts are automatically redirected to come from there instead. The point here being, you may be able to get away with not specifying the assembly in all of those mappings. But, try it out, don't take my word for it.

This has been an InfinitiesLoop answer. I now return you to your regularly scheduled programming :)

这篇关于使用的ScriptManager / ToolkitManager AjaxControlToolkit中加载脚本从微软的CDN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆