现在有asp.net 2.0 程序中使用asp.net ajax

对于现有的基于ASP.NET 2.0的Web应用程序,我们需要手动配置ASP.NET AJAX的相关设定。

1.添加必要程序集的引用

首先在解决方案中添加启用ASP.NET AJAX所必需的Microsoft.Web.Extensions.dll和System.Web.Preview.dll两个程序集的引用,这两个程序集分别随ASP.NET AJAX的“核心”部分和“Futures”部分的安装部署到了本机。注意,若本机和远程服务器均安装了ASP.NET AJAX的核心部分,则System.Web.Extensions.dll程序集就已经添加至了各自的GAC中,在接下来的步骤中就不必再添加该程序集的引用。

在Visual Studio中打开这个现有的ASP.NET 2.0的Web应用程序。若在Solution Explorer中没有\Bin文件夹,则首先在Web Site上单击右键,并在弹出的上下文菜单中选择Add ASP.NET Folder,然后在展开的菜单中选择Bin,即可添加Bin文件夹(见图2-7)。

接着在刚刚添加的\Bin文件夹上再次单击右键,并在弹出菜单中选择Add Reference…。在弹出的对话框中切换至Browse选项卡,并导航到System.Web.Extensions. dll和Microsoft.Web.Preview.dll这两个程序集所在的位置(按照默认路径,这两个文件一般位于C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\ v.1.0.61025\和C:\Program Files\Microsoft ASP.NET\ ASP. NET 2.0 AJAX Futures January CTP\ v.1.0.61025中)。选择这两个文件,如图2-8所示。然后点击OK按钮以确认添加引用。

更简单的方法是直接将Microsoft.Web.Extensions.dll和Microsoft.Web.Preview.dll拷贝到\Bin文件夹中,然后刷新Solution Explorer。如果一切正确的话,之后将会在Solution Explorer中看到添加好的这两个程序集,如图2-9所示。

图2-8    在Bin文件夹内添加System.Web. Extensions.dll程序集的引用

图2-9    添加了System.Web.Extensions.dll和Microsoft. Web.Preview.dll引用后的Solution Explorer

2.配置Web.Config文件

下面让我们打开并按照如下步骤编辑位于Web Site根目录下的Web.Config文件。

Web.Config是一个XML文件,其中的根元素是<configuration>。在<configuration>根元素中添加一个<configSections>子元素,并在其中定义一些嵌套的<sectionGroup>:

 
<configSections>

    
<sectionGroup name="system.web.extensions"  type="System.Web.Configuration.SystemWebExtensionsSectionGroup,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35">

    
<sectionGroup name="scripting"  type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

    
<sectionGroup name="webServices"  type="System.Web.Configuration.ScriptingWebServicesSectionGroup,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35">

          ...

          ...

        
</sectionGroup>

      
</sectionGroup>

    
</sectionGroup>

</configSections>

 

然后在<configuration>根元素中定义<System.web.extensions>子元素,并在其中定义相应的与上面<sectionGroup>相呼应的层次结构,如下所示:

 

<system.web.extensions>

    
<scripting>

      
<webServices>

        ...

        ...

      
</webServices>

    
</scripting>

</system.web.extensions>

 

若你希望ASP.NET AJAX应用程序在运行时能够对客户端脚本文件进行压缩或缓存,则应该在<sectionGroup name="scripting">中定义一个新的<section>:

 

<section name="scriptResourceHandler"  type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35" requirePermission="false"  allowDefinition="MachineToApplication"/>

 

然后在<scripting>中添加这部分启用脚本资源压缩以及缓存的配置声明:

 

<scriptResourceHandler enableCompression="true" enableCaching="true" />

 

这段声明将在程序运行于Release模式时压缩客户端脚本库的大小。如果不是程序中有非常特殊的需要,我们绝对应该添加这个HTTP模块以提高网络传输速度。

若ASP.NET AJAX应用程序中需要自定义的JSON转换方式,则应该在<sectionGroup name="webServices">的最内层中定义相应的<section>,例如:

 

<section name="jsonSerialization"  type="System.Web.Configuration.ScriptingJsonSerializationSection,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35" requirePermission="false"  allowDefinition="Everywhere"/>

 

并在<webServices>中添加自定义的转换器声明。例如,下列转换器即可将服务器端ADO.NET中的DataSet、DataTable以及DataRow自动转换为客户端的相应对象。当然,客户端的这三种对象也可以自动转换为服务器端的相应形式(关于这部分内容,将在第Ⅱ卷中详细介绍):

 

<jsonSerialization maxJsonLength="500">

    
<converters>

        
<add name="DataSetConverter"  type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

        
<add name="DataRowConverter"  type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

        
<add name="DataTableConverter"  type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

    
</converters>

</jsonSerialization>

 

若ASP.NET AJAX应用程序中需要在客户端访问ASP.NET所提供的用户身份认证服务,则应该在<sectionGroup name="webServices">中定义这样一个<section>:

 

<section name="authenticationService"  type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"  allowDefinition="MachineToApplication"/>

并在
<webServices>中添加这一段的定义,用来允许在客户端访问户身份认证服务(其中requireSSL属性可以根据需要设置):

<authenticationService enabled="true" requireSSL ="true|false"/>

 

若ASP.NET AJAX应用程序中需要在客户端访问ASP.NET所提供的用户个性化信息服务,则应该在<sectionGroup name="webServices">中定义这样一个<section>:

 

<section name="profileService"  type="System.Web.Configuration.ScriptingProfileServiceSection,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35" requirePermission="false"  allowDefinition="MachineToApplication"/>

 

并在<webServices>中添加这一段的定义,用来允许在客户端访问用户个性化信息服务。其中writeAccessProperties指定的用户个性化属性可以在客户端进行设定,read- AccessProperties指定的用户个性化属性可以在客户端进行访问:

 

<profileService enabled="true" 

          readAccessProperties
=" propertyname1;propertyname2" 

          writeAccessProperties
=" propertyname1;propertyname2" />

 

若想暴露出所有的用户个性化属性供客户端设定/读取,则可以在readAccess- Properties或WriteAccessProperties中使用通配符*,代码如下:

 

<profileService enabled="true" readAccessProperties="*" WriteAccessProperties="*" />

 

接下来在<configuration>根元素中的<system.web>节中注册ASP.NET AJAX服务器端控件的前缀:

 

<pages>

    
<controls>

      
<add tagPrefix="asp" namespace="System.Web.UI"  assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

      
<add tagPrefix="asp" namespace="Microsoft.Web.Preview.UI"  assembly="Microsoft.Web.Preview"/>

      
<add tagPrefix="asp" namespace="Microsoft.Web.Preview.UI.Controls"  assembly="Microsoft.Web.Preview"/>

    
</controls>

</pages>

 

接下来修改<compilation>节,允许调试和添加必要的用于调试的程序集:

 

<compilation debug="true">

    
<assemblies>

      
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

    
</assemblies>

    
<buildProviders>

      
<add extension="*.asbx"  type="Microsoft.Web.Preview.Services.BridgeBuildProvider"/>

    
</buildProviders>

</compilation>

 

注意,上面代码中同样包含了<buildProviders>以及其中*.asbx文件的声明。若你的应用程序不需要服务器端远程Web Service桥,那么没有必要添加<buildProviders>中的这部分内容。

还是在<system.web>元素中,若你的ASP.NET AJAX应用程序中需要在客户端调用服务器端的Web Service,则需要对Web Service应用一些新的HTTP Handler,并为其生成必要的客户端JavaScript代理脚本。找到<system.web>元素中的<httpHandlers>子元素,移除现有的Web Service文件(.asmx)的Handler,然后添加ASP.NET AJAX引入的新的Handler(*.asbx一项同样可以根据需要有选择地添加):

 

<httpHandlers>

    
<remove verb="*" path="*.asmx"/>

    
<add verb="*" path="*.asmx" validate="false"  type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,  Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

    
<add verb="*" path="*_AppService.axd" validate="false"  type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 

    
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  validate="false"/>

    
<add verb="GET,HEAD,POST" path="*.asbx"  type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"  validate="false"/>

</httpHandlers>

 

仍然是在<system.web>元素中添加如下的HTTP模块声明:

 

<httpModules>

    
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

</httpModules>

 

最后,在<configuration>节中添加<system.webServer>小节,为Web服务器进行相应的配置(这些配置是可选的,将只应用于IIS7中):

 

<system.webServer>

    
<validation validateIntegratedModeConfiguration="false"/>

    
<modules>

      
<add name="ScriptModule" preCondition="integratedMode"  type="System.Web.Handlers.ScriptModule, System.Web.Extensions,  Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

    
</modules>

    
<handlers>

      
<remove name="WebServiceHandlerFactory-Integrated"/>

      
<add name="ScriptHandlerFactory" verb="*" path="*.asmx"  preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

      
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd"  preCondition="integratedMode" type="System.Web.Script.Services.  ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

      
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD"  path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35"/>

      
<add name="ASBXHandler" verb="GET,HEAD,POST" path="*.asbx"  preCondition="integratedMode" type="System.Web.Script.Services.  ScriptHandlerFactory,System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>

    
</handlers>

</system.webServer>

通过上述设置过程,我们就完成了为现有的ASP.NET 2.0应用程序应用ASP.NET AJAX所需要的所有配置步骤。

可以看到ASP.NET AJAX的配置并不是那么容易,但新建的ASP.NET AJAX Web站点中的Web.config文件即包含了默认的ASP.NET AJAX配置。我们完全可以基于这个文件并按照上面的介绍,根据需要对其进行修改或补充。限于篇幅,这里就不再列出完整的代码。

posted @ 2007-11-09 14:58  chinaifne  阅读(978)  评论(0编辑  收藏  举报