Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ServiceStack support reverse routing?

Following REST it is advisable that API is discoverable and should be interlinked.

Does ServiceStack support any kind of reverse routing? I'm looking for something like Url.RouteLink in ASP MVC.

like image 754
Kugel Avatar asked Dec 28 '12 13:12

Kugel


1 Answers

There's some mixed concepts stated here.

  1. In and effort of trying to comply with REST you wish to embed URI's in your responses.
  2. How best to achieve embedding URI's in your responses. You've assumed a "Reverse Routing" mechanism is how this should be done.

REST style vs Strong-typing

I want to be explicitly mention here that one doesn't imply the other. On the one hand you're looking to follow a REST architecture (with whatever system you're building) and on the other you wish to follow strong-typing practices of using a typed-API, in this case to generate the external URI's of your API. These are somewhat contrasting styles as REST doesn't promote typed-APIs preferring to instead bind to the external URI surface of your APIs and loosely-typed Content-Types. Whilst a strong-typed language would recommend binding to a typed API, e.g. like the end-to-end typed API ServiceStack supports out-of-the-box.

Generating strong-typed URI's in ServiceStack

There's no ASP.NET MVC "Reverse Routing" concept/functionality in ServiceStack explicitly, but you can re-use the same functionality that the .NET Service Clients uses to generate and use user-defined Custom routes. To use this, you need to specify your custom routes on the DTO's (i.e. with the [Route] attribute as opposed to the fluent API in AppHost), e.g:

[Route("/reqstars/search", "GET")]
[Route("/reqstars/aged/{Age}")]
public class SearchReqstars : IReturn<ReqstarsResponse>
{
    public int? Age { get; set; }
}

var relativeUrl = new SearchReqstars { Age = 20 }.ToUrl("GET");
var absoluteUrl = EndpointHost.Config.WebHostUrl.CombineWith(relativeUrl);

relativeUrl.Print(); //=  /reqstars/aged/20
absoluteUrl.Print(); //=  http://www.myhost.com/reqstars/aged/20

Or if you just want the Absolute Url you can use the ToAbsoluteUri Extension method:

var absoluteUrl = new SearchReqstars { Age = 20 }.ToAbsoluteUri();
like image 76
mythz Avatar answered Oct 07 '22 11:10

mythz