Question

I have a WebMethod that I use on a number of pages in my ASP.NET site. I would like to move the function out of the individual pages and just have them reference a "global" WebMethod function. 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. Any ideas?

Was it helpful?

Solution

As stated in MSDN

Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service

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.

The first way would be to use page inheritance. In ASP.NET a page a subclass of class 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.

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. This and this MSDN posts could help you understand what we are talking about. This microsoft tutorial could guide you on how to do it.

Hope I helped!

OTHER TIPS

here you go:http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx

Walkthrough: Creating and Using AJAX-Enabled Web Service

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top