How to implement partail respones

Hi

I want to know how to support web api like this
/dogs?fields=name,color,location
fields means i can select the response data properties.

And

I want to support multi-format like this (use dot)
dogs.json
/dogs/1234.json

And

I want to support version like this
/v1/dogs
/v2/dogs

Could you give me some advice how SS support my solution?
I want to update my api in the next month.

Yin

You can accept a comma-delimited collection by using a string collection property, e.g:

[Route("/dogs")]
public class SearchDogs {
    public List<string> Fields { get; set; }
}

Which will let you call it with:

/dogs?fields=name,color,location

Specifying Content-Negotiation by using an extension is already built-in where you can call the above Service with:

/dogs.json

And you can match this route:

/dogs/1234.json

With:

[Route("/dogs/{Id}")]
public class GetDog {
    public int Id { get; set; }
}

For versioning please see our recommended versioning strategy.

Although note that Message-based designs that ServiceStack promotes lend themselves to designing and backwards and forwards compatible DTO’s which don’t require versioning.

If you really wanted to support multiple versions within the same Web Application you would need to map the routes manually, e.g:

[Route("/v1/dogs")]
public class SearchDogsV1 {
    public List<string> Fields { get; set; }
}

[Route("/v2/dogs")]
public class SearchDogsV2 {
    public List<string> Fields { get; set; }
}

But this is not something I’d recommend since I consider it unnecessary and causes a lot of friction in a statically typed language.

Thank you.

I want to know how to support web api like this
/dogs?fields=name,color,location
fields means i can select the properties of response data.

for example:

class Dog{
string name;
string color;
string location;
string XXX1;
string XXX2;
}

when i search the dogs, i just want to get three properties, should i create another DTO, such as
class PartialDog{
string name;
string color;
string location;
}
Is AutoQuery support this? I skip a lot of document and mix some concepts…

There’s no support for partially populating a Response DTO, you’d need to look at the fields in request.Fields collection and use that to work out which fields you want to populate.