简体   繁体   English

如何使用Vb.net创建“全局” WebMethod

[英]How to create a “global” WebMethod using Vb.net

I have a WebMethod that I use on a number of pages in my ASP.NET site. 我有一个WebMethod,可在ASP.NET站点的许多页面上使用。 I would like to move the function out of the individual pages and just have them reference a "global" WebMethod function. 我想将函数移出各个页面,而让它们引用“全局” WebMethod函数。 I've done this with numerous "normal" static functions that I use throughout the site by just creating a class and placing them in there. 我通过在整个站点中使用大量的“常规”静态函数来做到这一点,只需创建一个类并将它们放在其中即可。 But this approach doesn't seem to work with a WebMethod function. 但是这种方法似乎不适用于WebMethod函数。 Any ideas? 有任何想法吗?

As stated in MSDN MSDN中所述

Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service 将WebMethod属性附加到Public方法表示您希望该方法作为XML Web服务的一部分公开

If I get it right, this is not what you want to do, as this practice is oriented in exposing the functionality to outside consumers. 如果我做对了,这不是您想要做的,因为这种做法的目的是将功能暴露给外部消费者。

I would suggest three ways to avoid repeating the common functionality in ASP.NET. 我将提出三种避免重复ASP.NET中通用功能的方法。

The first way would be to use page inheritance . 第一种方法是使用页面继承 In ASP.NET a page a subclass of class System.Web.UI.Page . 在ASP.NET中,页面是System.Web.UI.Page类的子类。 Therefore, you could create a BasePage having all the common functionality, and your pages could inherit it, inheritting the respective functionality. 因此,您可以创建一个具有所有常用功能的BasePage ,并且您的页面可以继承它,并继承各自的功能。

The second way, would be to use the decorator design pattern . 第二种方法是使用装饰器设计模式 This design pattern would allow you to move common functionality in one place and decorating specific pages with it. 通过这种设计模式,您可以将通用功能移到一个位置并用它来装饰特定页面。 It is not the simplest thing to do and therefore it would not be my first suggestion. 这不是最简单的事情,因此这不是我的第一个建议。

The third way is using an HTTPHandler . 第三种方法是使用HTTPHandler This and this MSDN posts could help you understand what we are talking about. 这个这个 MSDN帖子可以帮助您了解我们在说什么。 This microsoft tutorial could guide you on how to do it. 此Microsoft教程可以指导您如何做。

Hope I helped! 希望我能帮上忙!

here you go: http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx 在这里您可以前往: http : //msdn.microsoft.com/zh-cn/library/bb532367(v=vs.90).aspx

Walkthrough: Creating and Using AJAX-Enabled Web Service 演练:创建和使用启用AJAX的Web服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM