How to return an inherited class? Getting a NULL response...

Topics: Web Api
Aug 5, 2011 at 9:44 PM

I am having an issue returning types that are inherited. The response is empty. Is this possible? Am I missing an attribute somewhere? Will the client code work as well? If I just return an Action class then it all works fine.


Here is the code...

 

public class Action {
}

public class UrlAction : Action {
    public string Url {get; set;}
}

public class TextAction : Action {
    public string Text {get; set;}
}

public class Work {
    public Action Action {get; set;}
}

[WebInvoke(UriTemplate = "", Method = "GET")]
public HttpResponseMessage<Work> GetWork(HttpRequestMessage request) {
    UrlAction action = new UrlAction(){Url = "Bob"};

    return new HttpResponseMessage<T>(action, HttpStatusCode.OK);
}

 

 

Aug 5, 2011 at 9:45 PM

My pseduo code had an error, here is the real method...

 

[WebInvoke(UriTemplate = "", Method = "GET")]
public HttpResponseMessage<Work> GetWork(HttpRequestMessage request) {
    UrlAction action = new UrlAction(){Url = "Bob"};
    Work work = new Work(){Action=action};

    return new HttpResponseMessage<T>(work, HttpStatusCode.OK);
}