在ASP.NET中替换Sys.Services的方法

开发 后端
本文介绍在ASP.NET中替换Sys.Services的方法,写一个类替换Sys.Services._ProfileService 类,甚至完全重写一个类,这个一般就看具体情况了。

使用自定义类替换Sys.Services.ProfileService对象

一般来说,这是最容易想到的办法。我们可以写一个类替换Sys.Services._ProfileService 类(这个类完全通过 prototype扩展,因此对于继承非常友好),甚至完全重写一个类,这个一般就看具体情况了。假设我们已经定义了这么一个类 “Jeffz.Services.ProfileService”,并将其包含在MyProfile.Service.js中,就要开始使用了。那么还要注意些什么呢?

需要注意的就是顺序,我们一般会使用ScriptManager引入该JS,如下:

  1. <asp:ScriptManager ID="ScriptManager1" runat="server" 
    EnablePartialRendering="false"> 
  2. <Scripts> 
  3. <asp:ScriptReference Path="MyProfileService.js" /> 
  4. Scripts> 
  5. <ProfileService LoadProperties="ZipCode, Address.City" 
    Path="MyProfile.asmx"/> 
  6. asp:ScriptManager> 


我们为ProfileService节点加上了LoadProperties属性,表明需要预加载Profile中的ZipCode和Address这个 Profile Group下的City属性。另外,我们将EnablePartialRendering属性设为了False,避免出现多余的代码。

  1. <script src="/Value-Add-WebSite/WebResource.axd?d=...;t=..." type=
    "text/javascript"></script> 
  2. <script type="text/javascript"> 
  3. </script> 
  4. <script src="MyProfileService.js" type="text/javascript"></script> 

***行引入的是 MicrosoftAjax.js,它之中定义了ASP.NET AJAX中默认的ProfileService,而紧接着就是对于ProfileService的使用:设定其Path以及预加载的 Properties。在引入之后千万不能忘了要将这些信息进行保留。但是这两者之间无法插入任何代码,因此我们可以在 MyProfileService.js里添加如下的代码,以保留这些信息:

  1. var path = Sys.Services.ProfileService.get_path();  
  2. if (!path)  
  3. {  
  4. path = Sys.Services._ProfileService.WebServicePath;  
  5. }  
  6. var properties = Sys.Services.ProfileService.properties;  
  7. var newnewInstance = new Jeffz.Services.ProfileService();  
  8. newInstance.set_path(path);  
  9. newInstance.properties = properties;  
  10. Sys.Services.ProfileService = newInstance

当然,可能代码会根据实际情况略有不同,但是注意JavaScript引入以及执行的顺序,在做任何自定义工作时都是非常重要的。

  有人也许会问,既然已经重新定义了自己的实现,为什么还要将其“伪装”成默认的ProfileService呢?因为这种“自定义”其实并不为“官方” 所承认,这么做能够保证了兼容性,保证了第三方的组件也能使用Profile Service,即使它们没有“意识”到没有使用ASP.NET AJAX提供的默认Profile Service。以上介绍在ASP.NET中替换Sys.Services的方法

【编辑推荐】

  1. ASP.NET中的WebRequestExecutor
  2. IIS6的ASP.NET ISAPI请求处理过程
  3. ASP.NET中的备份
  4. 浅谈ASP.NET ISAPI
  5. ASP.NET的IIS ISAPI extension
责任编辑:佚名 来源: IT168
相关推荐

2009-07-24 16:05:05

调用Web ServiASP.NET

2009-07-21 10:24:21

.NET RIA SeASP.NET

2011-06-08 11:36:16

ASP.NETrender

2009-02-20 09:19:56

AJAXASP.NETWeb Service

2009-01-16 13:17:16

AjaxASP.NET.NET

2009-07-28 13:26:34

Render方法ASP.NET

2009-07-29 15:51:29

ASP.NET中执行w

2009-07-23 16:44:51

AdRotator控件ASP.NET

2009-07-30 13:33:55

ASP.NET中的co

2009-07-27 09:29:38

ASP.NET中Jav

2009-01-04 13:27:10

URL RewriteIISASP.NET

2009-12-25 10:36:10

ASP.NET 和 X

2009-03-13 09:48:33

ASP.NETAjaxJQuery

2009-07-24 10:10:22

ASP.NET

2009-07-24 16:17:42

WebRequestEASP.NET

2011-06-08 11:05:38

getpost

2009-06-12 09:24:34

ASP.NET窗体ASP.NET MVC

2009-05-31 08:54:22

WEB开发ASP.NETApplication

2009-07-20 16:04:37

ASP.NET fra

2009-07-22 09:11:02

Action方法ASP.NET MVC
点赞
收藏

51CTO技术栈公众号