ServiceStack是否支持反向路由? [英] Does ServiceStack support reverse routing?

查看:63
本文介绍了ServiceStack是否支持反向路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在REST之后,建议可发现API,并且应该将其互连。

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

ServiceStack是否支持任何类型的反向路由?我正在ASP MVC中寻找类似 Url.RouteLink 的东西。

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.


  1. 在尝试遵守REST的过程中,您希望在响应中嵌入URI。

  2. 如何最好地在响应中嵌入URI。您已经假定应采用反向路由机制。



REST样式与强类型



我想明确提及在这里,一个并不暗示另一个。一方面,您希望遵循REST体系结构(无论您正在构建的系统是什么),另一方面,您希望遵循使用类型化API的强类型实践,在这种情况下,将生成外部URI。您的API。这些样式有些不同,因为REST不会提倡类型化API,而是倾向于绑定到API的外部URI表面和松散类型的Content-Type。虽然强类型语言会建议绑定到类型化API,例如像端到端类型化API 一样,ServiceStack支持开箱即用。

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.

ServiceStack中没有ASP.NET MVC的反向路由概念/功能,但是您可以重复使用 .NET服务客户端用于生成和使用用户的功能。定义的自定义路线。要使用此功能,您需要在DTO上指定自定义路由(即,使用 [Route] 属性,而不是AppHost中的Fluent API),例如:

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

或者如果您只需要绝对URL,就可以使用 ToAbsoluteUri 扩展方法:

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

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

这篇关于ServiceStack是否支持反向路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆