Does ServiceStack support reverse routing?

After REST, it is recommended that the API is discoverable and should be connected.

Does ServiceStack support any reverse routing? I am looking for something like Url.RouteLink in ASP MVC.

+6
source share
1 answer

There are several mixed concepts here.

  • In an attempt to perform REST, you want to embed a URI in your responses.
  • What is the best way to get URIs embedded in your answers. You suggested that the β€œreverse routing” mechanism is how it should be done.

REST style vs Strong typing

I want to directly point out here that this does not mean otherwise. On the one hand, you want to follow the REST architecture (using any system you build), and on the other hand, you want to follow the rules of typical typing of a typed API, in this case, to create an external URI for your API. These are somewhat contrasting styles, because REST does not promote typed APIs, preferring instead to snap to the outer surface of the URIs of your APIs and not typically typed Content-Types. While a strongly typed language will recommend binding to a typed API, for example. for example, terminating APIs . ServiceStack supports pre-release versions.

Creating a Strongly Typed URI in a ServiceStack

ServiceStack does not have the ASP.NET MVC "Reverse Routing" concept, but you can reuse the same functions as the .NET Service Clients to create and use custom routes. To use this, you need to specify your custom routes in the DTO (that is, with the [Route] attribute as opposed to the free API in AppHost), for example:

 [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 an Absolute URL, you can use the ToAbsoluteUri extension ToAbsoluteUri :

 var absoluteUrl = new SearchReqstars { Age = 20 }.ToAbsoluteUri(); 
+8
source

All Articles