使用ASP.NET AJAX的Profile Service


  本文标签:Profile Service ASP.NET AJAX

  在ScriptManager中指定Profile Service的Path

  在ASP.NET AJAX的客户端脚本中,如果没有使用Sys.Services.ProfileService.set_path方法来指定一个提供Profile Service的地址,就会使用默认的地址,它会使ASP.NET AJAX的Profile Service使用程序集中特定的类  。一般来说,我们不需要手动调用Sys.Services.ProfileService.set_path方法,只需要在ScriptManager中指定即可  。如下:

  1. <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug"> 
  2. <ProfileService Path="CustomProfileService.asmx" /> 
  3. asp:ScriptManager> 

  
实现自己的Web Service类

  指定了自己的Web Service类,自然就要实现自己的类了  。事实上,我们要实现的就是3个方法  。就这个方面来说,ASP.NET AJAX的Profile Service使用的默认的Web Service类Microsoft.Web.Profile.ProfileService是我们绝佳的参考  。因此,我们在这里分析一下这些方法,对于我们的自定义工作是非常有帮助的  。

  可能需要注意的一点是,我们在实现这些方法时,从理论上来讲参数类型不用完全和 Microsoft.Web.Profile.ProfileService中的方法完全相同  。ASP.NET AJAX的能够根据参数的类型尽可能地将获得的JSON字符串转换成需要的类型  。不过事实上,似乎 Microsoft.Web.Profile.ProfileService里那些方法的参数选择已经是非常合理的  。另外,由于客户端Profile Service代码不太容易修改(事实上客户端也不是不能扩展,最极端的情况,不就是我们自己实现一个ProfileService吗?),为了保持返回的JSON字符串能够被正确处理,这些方法的返回值一般来说可以不变  。

  GetAllPropertiesForCurrentUser方法

  这个方法的作用是获得当前用户所有的Profile信息,它没有输入的参数,返回的JSON字符串形式如下:

  1. {  
  2. ZipCode : ...,  
  3. Address.City : ...,  
  4. Address.State : ...  

  它通过GroupName.ProfileName的形式来表示Profile Group,Group中的每一个Profile需要分别列出,而“...”则表示对应Profile值的JSON字符串  。

  在Microsoft.Web.Profile.ProfileService里,这个方法的代码如下:

  1. [WebMethod]  
  2. public IDictionary<string, object> GetAllPropertiesForCurrentUser()  
  3. {  
  4. ProfileService.CheckProfileServicesEnabled();  
  5. return ProfileService.GetProfile(HttpContext.Current, null);  

  GetPropertiesForCurrentUser方法

  这个方法的作用是获得当前用户指定的Profile信息,它的输入JSON字符串形式如下:
[ZipCode, Address.City, Address.State]

  它的返回值的JSON字符串和GetAllPropertiesForCurrentUser相同,就不再赘述了  。以上介绍了使用ASP.NET AJAX的Profile Service  。