C# Rest API return dynamic object

Viewed 5305

I have an webservice

WebServiceHost webServiceHost= new WebServiceHost(typeof(WebMethods), new Uri(url));
webServiceHost.Open();

public class Fish { public string name = "I am a fish"; }
public class Dog { public int legs = 4; }
public class Cat { public DateTime dt = DateTime.Now;}

One of my webMethods should return a dynamic object

WebMethod:

Solution 1

[OperationBehavior]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/isTest?class={cl}")]
object isTest(string cl)
{
    object obj;

    switch (cl)
    {
        case "fish":
            obj= new Fish();
            break;
        case "dog":
            obj= new Dog();
            break;
        default:
            obj= new Cat();
            break;

    }
    return obj;

}

Solution 2

[OperationBehavior]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/isTest?class={cl}")]
dynamic isTest(string cl)
{
    dynamic obj;

    switch (cl)
    {
        case "fish":
            obj= new Fish();
            break;
        case "dog":
            obj= new Dog();
            break;
        default:
            obj= new Cat();
            break;

    }
    return obj;
}

Both are not working. The response is ERR_CONNECTION_RESET

Any idea how to realise it? Thanks for help.

2 Answers
Related