Wednesday, March 21, 2012

Trying to pass JSON objects to C# methods

Hello,

I am trying to overload a webmethod unsuccessfully. I recall reading that webmethods can not be overloaded since SOAP does not support that. If I only intend to make my webmethods accessible from javascript/JSON is there a way to force ASP.NET to accept overloaded webmethods?

My current workaround uses a brokering method that actually test the type (from __Type) and branches accordingly.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[PrincipalPermission(SecurityAction.Demand, Role = "Users")]
public status save(object obj)
{
status stat;
switch (obj.ToString())
{
case "collection":
stat = collectionSave((collection)obj);
break;
case "bookmark":
stat = bookmarkSave((bookmark)obj);
break;
default:
stat = new status(999, "save(): did not find method for " + obj.ToString());
break;
}

return stat;
}

On my client I can simply call save() and pass in any of the allowable JSON objects that exist on the page. However, this seems to be a klunky way of doing things. Is there a better way? If I create overridden methods the compiler does not complain, but it seems that only the final method in the list of methods is the one called, regardless of the type passed in.

Thanks,

Troy

The main problem seems to be thatwebmethods can not be overloaded since SOAP does not support it.

I'm afraid that we haven't any better ways to do that.

No comments:

Post a Comment