共用方式為


HOW TO:設定 ASP.NET AJAX 中的 ASP.NET 服務

更新:2007 年 11 月

本主題說明如何在具備 AJAX 技術的 ASP.NET 用戶端應用程式中,設定其組態以呼叫 ASP.NET Web 服務 (.asmx 檔案)。本主題會在接下來的小節中詳細描述這些工作:

  • 設定 Web 服務以透過用戶端指令碼啟用呼叫。

  • 設定 JSON 序列化。

  • 設定驗證服務。

  • 設定角色服務。

  • 設定設定檔服務。

組態設定全是在 system.web.extension 組態群組中完成。For more information, see system.web.extensions 項目 (ASP.NET 設定結構描述).

設定 Web 服務以透過用戶端指令碼啟用呼叫

下列程序說明如何設定 ASP.NET Web 服務,以便透過用戶端指令碼加以呼叫。如需詳細資訊,請參閱將 Web 服務公開給用戶端指令碼

對於不是從具備 ASP.NET AJAX 的用戶端 (指令碼) 發出的 Web 服務呼叫,ScriptHandlerFactory 處理常式類別會將呼叫委派給使用 SOAP (不是 JSON) 格式的預設處理常式。這項行為會在內部完成,您不必採取任何動作。此外,您也可以停用 Web 服務的 SOAP 通訊協定。

若要停用 Web 服務的 SOAP 通訊協定

  • 在網站的 Web.config 檔案中,清除 Web 服務的所有通訊協定,如下列範例所示:

    <system.web>
      <webServices>
        <protocols>
          <clear/>
        </protocols>
      </webServices>
    </system.web>
    

若要設定 Web 服務以透過用戶端指令碼啟用呼叫

  • 在網站的 Web.config 檔案中,註冊 ScriptHandlerFactory HTTP 處理常式。

    這個處理常式會處理指令碼對 Web 服務的呼叫。

    注意事項:

    對於您在 Microsoft Visual Studio 2005 中所建立之任何新的具備 AJAX 能力的網站,這些組態設定是 Web.config 檔案範本的一部分。 

    下列範例示範用於註冊處理常式的 Web.config 項目。

    <system.web>
      <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx"
          type="System.Web.Script.Services.ScriptHandlerFactory"
           validate="false"/>
      </httpHandlers>
    <system.web>
    

設定 JSON 序列化

當用戶端指令碼呼叫 ASP.NET Web 服務時,資料是以 JSON 格式進行交換。您可以透過 jsonSerialization 項目來設定 JSON 序列化設定。

若要設定 JSON 序列化

  1. 開啟網站的 Web.config 檔案。

    注意事項:

    如果您在 Microsoft Visual Studio 2005 建立 Web 服務,則 Web.config 檔會包含 system.web.extensions 區段,且子項目會標記為註解。

  2. 在 webServices 項目中加入 jsonSerialization 項目。

    這個項目能夠讓您指定自訂轉換子並覆寫預設值。

  3. 定義下列序列化屬性。所有屬性都是選擇性的。

    • recursionLimit. 指定要序列化型別的最大深度。預設遞迴限制為 100。

    • maxJsonLength. 指定 JSON 字串的最大長度 (UTF-8 字元的最大數目)。預設長度為 102400。

    下列範例說明如何設定 jsonSerialization 項目。在此範例中,maxJsonLength 設定為 5000。

    <configuration>
      <system.web.extensions>
        <scripting>
          <webServices>
            <jsonSerialization maxJsonLength="5000"/>
          </webServices>
        </scripting>
      </system.web.extensions>
    </configuration>
    
  4. 如果您想要使用自訂轉換子,請在 jsonSerialization 項目中加入 converters 項目。

    此項目需要下列屬性:

    • name:唯一的識別項。

    • type:轉換子的完整型別名稱。

    下列範例示範如何設定自訂轉換子。

    <configuration>
      <system.web.extensions>
        <scripting>
          <webServices>
            <jsonSerialization maxJsonLength="50"/>
              <converters>
                <add name="MyCustomConverter" 
                  type="MyCompany.ConvertersNameSpace.MyTypeConverter"/>
              </converters>
            </jsonSerialization>
          </webServices>
        </scripting>
      </system.web.extensions>
    </configuration>
    

設定應用程式服務以透過用戶端指令碼啟用呼叫

下列程序說明如何設定內建的應用程式服務,以便在具備 AJAX 能力的 Web 應用程式 (在瀏覽器中執行) 中透過指令碼啟用呼叫。

如需 ASP.NET 應用程式服務的詳細資訊,請參閱下列主題:

若要設定驗證服務

  1. 開啟網站的 Web.config 檔案。

  2. 在 authentication 項目中,啟用表單驗證。

    下列範例說明設定為使用表單驗證的 authentication 項目。未經過驗證的使用者對受保護資源的任何存取嘗試都會被重新導向至網站根目錄的 Login.aspx 頁面。

    <system.web>
      <authentication mode="Forms">
        <forms cookieless="UseCookies" 
          loginUrl="~/login.aspx"/>
      </authentication>
    <system.web>
    

    如需詳細資訊,請參閱搭配 ASP.NET AJAX 使用表單驗證

  3. 啟用 system.web.extensions 項目中的驗證服務。

    下列範例示範如何啟用驗證服務。

    <system.web.extensions>
      <scripting>
        <webServices>
           <authenticationService enabled="true" />
        </webServices>
      </scripting>
    </system.web.extensions>
    

若要設定角色服務

  1. 開啟網站的 Web.config 檔案。

  2. 啟用 system.web.extensions 項目中的角色服務。

    下列範例示範如何啟用角色服務。

    <system.web.extensions>
      <scripting>
        <webServices>
          <rolesService enabled="true" />
        </webServices>
      </scripting>
    </system.web.extensions>
    

若要設定設定檔服務

  1. 開啟網站的 Web.config 檔案。

  2. 如果設定檔屬性尚未定義,請針對您要在應用程式中公開的設定檔屬性加以定義。

    在 profile 區段中定義設定檔屬性時,您可以使用類似下列範例中的語法。請使用 group 項目定義群組屬性。

    <system.web>
      <profile enabled="true">
        <add name=" Backgroundcolor" type="System.String"
           defaultValue="white" />
        <add name=" Foregroundcolor" type="System.String"
         defaultValue="black" />
        <properties>
          <group name="Address">
           <add name="Street" type="System.String" />
           <add name="City" type="System.String"/>
           <add name="PostalCode" type="System.String" />
          </group>
        </properties>
      </profile>
    </system.web>
    

    如需詳細資訊,請參閱 ASP.NET 設定檔屬性概觀

  3. 啟用 system.web.extensions 項目中的設定檔服務。

    下列範例示範如何啟用設定檔服務。

    <system.web.extensions>
      <scripting>
        <webServices>
          < profileService enabled="true" />
        </webServices>
      </scripting>
    </system.web.extensions>
    
  4. 針對您要讓用戶端應用程式使用的每一個設定檔屬性,請將屬性 (Property) 名稱加入至 profileService 項目的 readAccessProperties 屬性 (Attribute)。

  5. 針對可透過用戶端指令碼進行更新的每個伺服器設定檔屬性 (Property),將屬性 (Property) 名稱加入至 writeAccessProperties 屬性 (Attribute)。

    下列範例顯示如何公開個別的屬性和設定用戶端應用程式是否可讀取和寫入這些屬性。

    <profileService enabled="true" 
        readAccessProperties="Backgroundcolor,Foregroundcolor" 
        writeAccessProperties=" Backgroundcolor,Foregroundcolor"/>
    

請參閱

工作

HOW TO:設定 ASP.NET AJAX 中的 WCF 服務

概念

ASP.NET 組態概觀

ASP.NET 組態檔階層架構和繼承

編輯 ASP.NET 組態檔

參考

system.web.extensions 項目 (ASP.NET 設定結構描述)

System.Web.Configuration

其他資源

ASP.NET 組態設定

ASP.NET 組態 API