chirag
chirag

Reputation: 1828

Return with different ResponseDTO according querystring parameter value in Servicestack?

I want to return different type of response according QueryString parameter value. Example

http://localhost:8080/myservice?Type=low --> return responseType1
http://localhost:8080/myservice?Type=high --> return responseType2

according Type value in URL, I want to different type of response in service.

Upvotes: 1

Views: 35

Answers (1)

mythz
mythz

Reputation: 143399

It is highly discouraged to change the return type based on how you call the Service. Your clients bind to the response of your Service -- changing it indiscriminately based on some runtime heuristic will break them.

But if you must, ServiceStack Services doesn't enforce a strict response type and lets you return any object, e.g:

public object Any(Request request)
{
    return request.Type == "low"
       ? responseType1
       : responseType2;
}

It does mean the Request DTO can no longer have a consistent IReturn<Response> interface marker and you'll now force the clients to know which Response is returned and when, which they'll have to maintain themselves on the call-site, e.g:

var response = client.Get<ResponseType>(new Request { Type = "low" });

This also affects metadata services which can't rely on the Service to have a consistent Response Type.

Upvotes: 2

Related Questions