I know it’s not possible to define a WebMethod in a user control. Typically you define a WebMethod on the Page class where the user control is dropped and somewhere in the Page’s code behind you see something like this…
Public Class SomePage Inherits Page ... <WebMethod()> Public Shared Function MyWebMethod(someObject As MyObject) As Results ... do some work ... Return someObject.SomeFunctionThatDoesSomething() End Function End Class
However, lately, I find myself doing this…note the filenames
Public Class MyUserControl Inherits UserControl ... End Class Partial Public Class SomePage <WebMethod()> Public Shared Function MyWebMethod(someObject As MyObject) As Results ... do some work ... Return someObject.SomeFunctionThatDoesSomething() End Function End Class
I like how this keeps the webmethods with the user control code instead of the individual pages. And it allows me to keep an “inventory” so to speak of pages using the code.
I also like how it sort of resolves the issue that “you can’t define a webmethod in a user control”
Now just to clarify, all my user Controls files are compiled with the main website and are not intended to be used anywhere else.
I would just like some opinions/criticism on using partial classes like this.
In all the years I’ve been using SO and researching user controls and WebMethods I’ve never come across this style of declaration.
It works, it suits my needs, but the fact that I’ve never seen it done has me questioning myself.