如何:在 ASP.NET AJAX 中配置 ASP.NET 服务

更新:2007 年 11 月

本主题介绍如何设置配置,以使支持 AJAX 的 ASP.NET 客户端应用程序可以调用 ASP.NET Web 服务(.asmx 文件)。本主题所描述的任务包括:

  • 配置 Web 服务以便从客户端脚本中对其进行调用。

  • 配置 JSON 序列化。

  • 配置身份验证服务。

  • 配置角色服务。

  • 配置配置文件服务。

这些配置设置都是在 system.web.extension 配置组中进行的。有关更多信息,请参见 system.web.extensions 元素(ASP.NET 设置架构)

配置 Web 服务以便从客户端脚本中对其进行调用

下面的过程介绍如何配置 ASP.NET Web 服务,以便从客户端脚本对其进行调用。有关更多信息,请参见向客户端脚本公开 Web 服务

对于不是从支持 AJAX 的 ASP.NET 客户端(脚本)发出的 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 服务的调用。

    Bb763183.alert_note(zh-cn,VS.90).gif说明:

    这些配置设置是您在 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 文件。

    Bb763183.alert_note(zh-cn,VS.90).gif说明:

    如果在 Microsoft Visual Studio 2005 中创建 Web 服务,Web.config 文件将包含 system.web.extensions 部分,其子元素已被注释掉。

  2. 在 webServices 元素中添加一个 jsonSerialization 元素。

    该元素使您可以指定自定义转换器和重写默认设置。

  3. 定义下列序列化属性。所有属性都是可选的。

    • 指定要序列化类型的最大深度。默认的递归限制为 100。

    • 指定 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 元素中启用 Forms 身份验证。

    下面的示例演示 authentication 元素,该元素配置为使用 Forms 身份验证。未经身份验证的用户访问受保护资源的任何尝试都将重定向到网站根目录中的 Login.aspx 页。

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

    有关更多信息,请参见将 Forms 身份验证用于 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),将属性 (Property) 名称添加到 profileService 元素的 readAccessProperties 属性 (Attribute)。

  5. 对于可以从客户端脚本更新的每个服务器配置文件属性 (Property),将属性 (Property) 名称添加到 writeAccessProperties 属性 (Attribute)。

    下面的示例演示如何公开各个属性并设置客户端应用程序是否可以读取和写入这些属性。

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

请参见

任务

如何:在 ASP.NET AJAX 中配置 WCF 服务

概念

ASP.NET 配置概述

ASP.NET 配置文件层次结构和继承

编辑 ASP.NET 配置文件

参考

system.web.extensions 元素(ASP.NET 设置架构)

System.Web.Configuration

其他资源

ASP.NET 配置设置

ASP.NET 配置 API