简体   繁体   English

ServiceStack是否支持反向路由?

[英]Does ServiceStack support reverse routing?

Following REST it is advisable that API is discoverable and should be interlinked. 在REST之后,建议API是可发现的,并且应该是相互关联的。

Does ServiceStack support any kind of reverse routing? ServiceStack是否支持任何类型的反向路由? I'm looking for something like Url.RouteLink in ASP MVC. 我在ASP MVC中寻找像Url.RouteLink这样的东西。

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. 在努力遵守REST的过程中,您希望在您的回复中嵌入URI。
  2. How best to achieve embedding URI's in your responses. 如何最好地在您的回复中嵌入URI。 You've assumed a "Reverse Routing" mechanism is how this should be done. 您假设“逆向路由”机制是应该如何完成的。

REST style vs Strong-typing REST样式与强类型

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. 一方面,您希望遵循REST架构(使用您正在构建的任何系统),另一方面,您希望遵循使用类型API的强类型实践,在这种情况下,生成外部URI的你的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. 这些是有些对比的样式,因为REST不会提升类型API,而是更喜欢绑定到API的外部URI表面和松散类型的内容类型。 Whilst a strong-typed language would recommend binding to a typed API, eg like the end-to-end typed API ServiceStack supports out-of-the-box. 虽然强类型语言会建议绑定到类型化的API,例如像端到端类型的API ServiceStack支持开箱即用。

Generating strong-typed URI's in ServiceStack 在ServiceStack中生成强类型URI

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. ServiceStack中没有明确的ASP.NET MVC“反向路由”概念/功能,但您可以重用.NET服务客户端用于生成和使用用户定义的自定义路由的相同功能。 To use this, you need to specify your custom routes on the DTO's (ie with the [Route] attribute as opposed to the fluent API in AppHost), eg: 要使用它,您需要在DTO上指定自定义路由(即使用[Route]属性而不是AppHost中的流畅API),例如:

[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: 或者,如果您只想要Absolute Url,可以使用ToAbsoluteUri Extension方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM