看過目前網路上找得到的MS AJAX Demo,清一色的做法都是新建一個"ASP.NET AJAX-Enabled Web Application"再開始動工。但是如果手上有已經開發到一半的Web Site Project或Web Application Project,又想要在上面加上AJAX功能,怎麼辦?  難道要砍掉重練嗎?

其實AJAX-Enabled Web Application的關鍵在於web.config中有些特殊的設定,只要將web.config照下面的方法修改(當然,要安裝ASP.NET 2.0 AJAX Extensions 1.0),一般的專案裡就可以加上AJAX Control囉!  大家不妨試試,如果有問題,歡迎留言提出。

<?xml version="1.0"?>
<configuration>
     <!-- STEP 1 - 加入configSections -->
     <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">
                     <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                     <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
                           <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
                           <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                           <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
                     </sectionGroup>
                </sectionGroup>
           </sectionGroup>
     </configSections>
     <!-- STEP 1 END -->
     <connectionStrings/>
     <system.web>
           <!-- STEP 2 - 加上Page中的asp:宣告,System.Web.UI改指向System.Web.Extension -->
           <pages>
                <controls>
                     <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                </controls>
           </pages>
           <!-- STEP 2 END -->
           <compilation debug="true">
                <assemblies>
                     <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                     <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                     <!-- STEP 3 - 加上System.Web.Extensions的Reference,也可以透過UI新增 -->
                     <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                     <!-- STEP 3 END -->
                </assemblies>
           </compilation>
           <!-- STEP 4 - 加入HTTP Handler及HTTP Module -->
           <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"/>
           </httpHandlers>
           <httpModules>
                <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
           </httpModules>
           <!-- STEP 4 END -->
           <authentication mode="Windows"/>
           <customErrors mode="Off"/>
     </system.web>
     <!-- STEP 5 - 加入system.webServer -->
     <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-ISAPI-2.0"/>
                <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"/>
           </handlers>
     </system.webServer>
     <!-- STEP 5 END -->
     <appSettings />
</configuration>
 

Comments

# by alan

您好我最近把之前的asp.net專案改成asp.net ajax的專案 雖已經依您提供的web.config修改 但在web.config檔上發生 下列錯誤,不知大大可否指點迷津一下 組態錯誤 描述: 處理服務此要求所需的組態檔時發生錯誤。請檢視下列的特定錯誤詳細資訊,並適當修改您的組態檔。 剖析器錯誤訊息: 無法辨認的屬性 'type'。 原始程式錯誤: <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

# by Jeffrey

To alan, 檢查一下IIS上WebApplication的ASP.NET的版本是否被誤設成1.1?

# by teddy

謝謝你的回覆, 我也正為這個問題頭痛, 原來是我的IIS設成1.1.

# by Lance

你好,想請問,我是用asp.net ajax的專案開發,但.... 在專案裡的web.config裡,requirePermission="false", 剖析器錯誤訊息說'requirePermission' 屬性未宣告,不知道是哪裡有問題?

# by

今天也遇到这个曾是 ,在网上参看了很多的例子,还是没有解决问题。为了一个ajax功能,得在web.config里添加这么多的节点,实在不敢恭维微软的设计

# by Jeffrey

to Lance, 有沒有可能是因為IIS把WebApp設成ASP.NET 1.1? to 峰, 如果是純Client端的效果(Modal Box, Accordion...),我現在也傾向用jQuery,輕巧多了。 ASP.NET AJAX最大的好處應該是讓完全不懂Javascript的麻瓜也可以寫出AJAX來,但在效能與複雜度上的確不是最佳的解決方案。如果你具有不錯的Javascript基準,倒是可以考量如jQuery之類的Framework。到ASP.NET 3.5後,AJAX會變成內建,也不會再需要做這些手工了。

Post a comment


39 - 14 =