Asp.Net Ajax 学习笔记16 Profile Service扩展方式

本文探讨了Ajax技术过度使用的潜在问题,包括增加服务器负担、降低用户体验及引入安全隐患等,并介绍了通过预加载减轻服务器负担的方法以及如何利用ProfileService进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次的课程首先说明了Ajax技术滥用的危害。

Ajax是一个很好的技术但是象很多事情一样,过度使用就会有一定的危害。

  1. Round-trip增多导致性能降低。Round-trip就是只提交到服务器和服务器回发的次数增多,这样,增大了服务器的响应次数,导致服务器负担增大。
  2. 交互性降低导致用户体验变差。主要的问题在于Ajax是无刷新的在处理页面,有的Ajax页面没有提供数据正在加载的提示,导致用户不清楚现在数据是否正在提交或者加载。
  3. 无端使用Ajax技术容易增加安全隐患。因为如果不给WebService加上授权访问,会给网站带来安全方面的问题
  4. Ajax技术是天生的搜索杀手。使用了Ajax的页面可能会导致搜索引擎的搜索程序不能找到。

为了减轻服务器的负担,Ajax应用种应该使用预加载,就是说在页面第一次访问服务器的时候,就将应该回发的数据传送到客户端,用户操作使用Ajax调用的时候不是从服务段取得数据,而是从客户端。

Profile Service的预加载

 

通过ProfileService的LoadProperties属性,进行Profile的预加载。

客户端调用ProfileSerivce的实质就是Asp.Net Ajax调用包装了Profile的WebService.

那么我们可以通过两种手段来扩展Profile。

1、扩展Asp.Net的Profile

自定义一个Profile类CustomProfile,他继承于System.Web.Profile.ProfileProvider类,CustomProfile类,需要实现以下抽象方法

  • Initialize
  • ApplicationName
  • GetPropertyValues
  • SetPropertyValues
  • DeleteProfiles
  • DeleteInactiveProfiles
  • GetAllProfiles
  • GetAllInactiveProfiles
  • FindProfilesByUserName
  • FindInactiveProfileByUserName
  • GetNumberOfInactiveProfiles

然后需要在WebConfig文件里指定ProfileProvider由哪个类来提供。如下例

 <profile enabled="true" automaticSaveEnabled="true" defaultProvider="CustomProvider">
      
<providers>
        
<clear />
        
<add name="CustomProvider"
             type
="CustomProvider"
             connectionStringName
="ProfileDatabase" 
             applicationName
="ProfileSample" 
             description
="Sample for ASP.NET profile and Profile Service" />
      
</providers>
      
<properties>
        
<add name="Name" type="System.String"/>
        
<add name="Email" type="System.String" />
        
<add name="Age" type="System.Int32" />
        
<group name="Address">
          
<add name="City" type="System.String" />
          
<add name="Street" type="System.String" />
          
<add name="PostalCode" type="System.String" />
        
</group>
      
</properties>
    
</profile>

当然这样继承ProfileProvider的方法,十分的繁琐,我们需要重写很多的基类。

那么Asp.Net Ajax访问Profile的时候,外部包装了一层WebService,我们可以重写这个类。

2、重写Ajax客户端访问Profile的WebService。

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CustomProfileService : System.Web.Services.WebService
{

    [WebMethod]
    
public IDictionary<stringobject> GetAllPropertiesForCurrentUser()
    
{
        
return null;
    }


    [WebMethod]
    
public IDictionary<stringobject> GetPropertiesForCurrentUser(string[] properties)
    
{
        
return null;
    }


    [WebMethod]
    
public int SetPropertiesForCurrentUser(IDictionary<stringobject> values)
    
{
        
return 0;
    }

}

GetAllPropertiesForCurrentUser方法:从当前用户得到所有的Properties,返回值是一个字典的集合,包含了由客户端传来的键值对应。

GetPropertiesForCurrentUser:从当前用户根据指定的Properties集合名字得到这些Properties字典已和。

SetPropertiesForCurrentUser:为当前的用户设置Properties集合。参数是一个关于这个用户的字典集合。

那么在实现这些方法之后,需要在ScriptManager里指定ProfileService的路径,告诉客户端调用这个Profile Web Service

<asp:ScriptManager ID="ScriptManager1" runat="server">
            
<ProfileService Path="CustomProfileService.asmx" />
</asp:ScriptManager>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值