Skip to content

RestRouteHandler details

Jark edited this page Jan 14, 2017 · 3 revisions

Controller creation types

You can choose to have one instance of your rest controller for the whole application cycle, or one per call. This is controlled by the RestController class attribute.

[RestController(InstanceCreationType.Singleton)]
[RestController(InstanceCreationType.PerCall)]

Url matching and parameters

You can use the UriFormatAttribute on your method to define the uri you want to match on. All strings between { and } will be handled as input parameter and should have a corresponding method parameter.

[UriFormat("/parameter/{id}")]
public IGetResponse GetParameter(int id)
{
...

Using arrays

Basic arrays are supported as uri parameter. It should be a ; delimited string and the matching method parameter should be of type IEnumerable<T> where T is one of the following: string, bool, datetime, char and all numbers (int, ushort etc).

[UriFormat("/parameter?filter={ids}")]
public IGetResponse GetParameter(IEnumerable<int> ids)

REST verb

The return type of your method is used to determine the verb of the REST request it will respond to. There are four available verb types:

  • IGetResponse (get)
  • IPostResponse (create)
  • IPutResponse (update)
  • IDeleteResponse (delete)

FromContent

You can use the FromContentAttribute on a method parameter. Restup will deserialize the http request body and use that as value for the parameter.

public IPutResponse GetParameter([FromContent]MyDataObject data)

Note: there can only be one FromContentAttribute per method and it should always be the last parameter.

Serializing/deserializing

There are two content types supported: xml and json. The .NET internal xml serializer is used for XML. For Json we used the incredible lib from Newtonsoft. By default all your types are serializable by both serializers. Just create a class with public properties and it will be serialized correctly.

Http request

If your http request has a body, the "Content-Type" header is used to determine the serializer.

Http response

The http request header "Accept" is used to determine the serializer.

Note: all serializers default to Json if headers are missing.