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.
There's some mixed concepts stated here.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With