使用ASP.NET AJAX的Profile Service
在ScriptManager中指定Profile Service的Path
在ASP.NET AJAX的客戶端腳本中,如果沒有使用Sys.Services.ProfileService.set_path方法來指定一個提供Profile Service的地址,就會使用默認的地址,它會使ASP.NET AJAX的Profile Service使用程序集中特定的類。一般來說,我們不需要手動調(diào)用Sys.Services.ProfileService.set_path方法,只需要在ScriptManager中指定即可。如下:
- <asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
- <ProfileService Path="CustomProfileService.asmx" />
- </asp:ScriptManager>
實現(xiàn)自己的Web Service類
指定了自己的Web Service類,自然就要實現(xiàn)自己的類了。事實上,我們要實現(xiàn)的就是3個方法。就這個方面來說,ASP.NET AJAX的Profile Service使用的默認的Web Service類Microsoft.Web.Profile.ProfileService是我們絕佳的參考。因此,我們在這里分析一下這些方法,對于我們的自定義工作是非常有幫助的。
可能需要注意的一點是,我們在實現(xiàn)這些方法時,從理論上來講參數(shù)類型不用完全和 Microsoft.Web.Profile.ProfileService中的方法完全相同。ASP.NET AJAX的能夠根據(jù)參數(shù)的類型盡可能地將獲得的JSON字符串轉(zhuǎn)換成需要的類型。不過事實上,似乎 Microsoft.Web.Profile.ProfileService里那些方法的參數(shù)選擇已經(jīng)是非常合理的。另外,由于客戶端Profile Service代碼不太容易修改(事實上客戶端也不是不能擴展,最極端的情況,不就是我們自己實現(xiàn)一個ProfileService嗎?),為了保持返回的JSON字符串能夠被正確處理,這些方法的返回值一般來說可以不變。
GetAllPropertiesForCurrentUser方法
這個方法的作用是獲得當前用戶所有的Profile信息,它沒有輸入的參數(shù),返回的JSON字符串形式如下:
- {
- 'ZipCode' : ...,
- 'Address.City' : ...,
- 'Address.State' : ...
- }
它通過GroupName.ProfileName的形式來表示Profile Group,Group中的每一個Profile需要分別列出,而“...”則表示對應(yīng)Profile值的JSON字符串。
在Microsoft.Web.Profile.ProfileService里,這個方法的代碼如下:
- [WebMethod]
- public IDictionary<string, object> GetAllPropertiesForCurrentUser()
- {
- ProfileService.CheckProfileServicesEnabled();
- return ProfileService.GetProfile(HttpContext.Current, null);
- }
GetPropertiesForCurrentUser方法
這個方法的作用是獲得當前用戶指定的Profile信息,它的輸入JSON字符串形式如下:
['ZipCode', 'Address.City', 'Address.State']
它的返回值的JSON字符串和GetAllPropertiesForCurrentUser相同,就不再贅述了。以上介紹了使用ASP.NET AJAX的Profile Service。
【編輯推薦】