简体   繁体   English

与Rest动词匹配的ServiceStack新API动作

[英]ServiceStack New API Actions matching Rest Verbs

With the older version SomeService : RestServiceBase can match OnGet OnPost OnPut OnDelete actions with the coresponding incoming verbs. 使用旧版本SomeService : RestServiceBase可以将OnGet OnPost OnPut OnDelete操作与相应的传入动词相匹配。

With the newer version, say I have the following: 对于较新的版本,请说我有以下内容:

//-----------------------------------------
[Route("/todos/{id}","GET")] //display request
[Route("/todos/{id}", "POST")] //edit request
public class Todo : IReturn<TodoResponse> {
    public long Id { get; set; }
    public string Content { get; set; }
}

public class TodoService : Service {
    public object Get(Todo request) { ... } // will GET verb know this Get() function?
    public object Post(Todo request) { ... }// will POST verb know this Post() function?
}

The Action names "Get" "Post" are no longer marked "override", how does SS match the correct verbs to hit Get() and Post() functions? 动作名称“获取”“发布”不再标记为“覆盖”,SS如何匹配正确的动词来命中Get()和Post()函数?

//-------------------------------------------------------------------------- // ------------------------------------------------ --------------------------

Or Round 2, now I have a modification... 或第2轮,现在我有一个修改......

//-----------------------------------------
[Route("/todos/{id}","GET")] //display request
public class DisplayTodo : IReturn<TodoResponse> {
    public long Id { get; set; }
}
[Route("/todos/{id}", "POST")] //edit request
public class EditTodo : IReturn<TodoResponse> {
    public long Id { get; set; }
    public string Content { get; set; }
}

public class TodoService : Service {
    //different request DTOs this time ...
    public object Get(DisplayTodo request) { ... } //again, same route "/todos/{id}" 
    public object Post(EditTodo request) { ... }   //will SS get confused about the verbs? 
}

Under the same route "/todos/{id}" how does SS distinguish verbs in the above cases? 在相同的路线“/ todos / {id}”中,SS在上述情况下如何区分动词?

Could you please sort me out with the 2 questions? 你可以用2个问题来解答我吗? Thank you! 谢谢!

ServiceStack will try to match both the request VERB and its request DTO. ServiceStack将尝试匹配请求VERB及其请求DTO。 If either one of them do not match, you'll get a no request handler message. 如果其中任何一个不匹配,您将获得无请求处理程序消息。

Your examples seem to be fine, have you encoutered any problem with them ? 你的例子似乎很好,你有没有遇到任何问题?

This is the Smart Routing section taken from the New API wiki page : 这是从新API维基页面获取的智能路由部分:

Matching Rules 匹配规则

For the most part you won't need to know about this as ServiceStack's routing works as you would expect. 在大多数情况下,您不需要了解这一点,因为ServiceStack的路由工作正如您所期望的那样。 Although this should still serve as a good reference to describe the resolution order of ServiceStack's Routes: 虽然这仍然可以作为描述ServiceStack路由的解析顺序的良好参考:

  1. Any exact Literal Matches are used first 首先使用任何精确的文字匹配
  2. Exact Verb match is preferred over All Verbs 精确动词匹配优于所有动词
  3. The more variables in your route the less weighting it has 路线中的变量越多,其权重就越小
  4. When Routes have the same weight, the order is determined by the position of the Action in the service or Order of Registration (FIFO) 当路由具有相同的权重时,顺序由服务中的操作位置或注册顺序(FIFO)确定

These Rules only come into play when there are multiple routes that matches the pathInfo of an incoming request. 当有多个路由与传入请求的pathInfo匹配时,这些规则才会发挥作用。

Lets see some examples of these rules in action using the routes defined in the new API Design test suite: 让我们使用新API设计测试套件中定义的路径查看这些规则的一些示例:

[Route("/reqstars")]
public class Reqstar {}

[Route("/reqstars", "GET")]
public class AllReqstars {}

[Route("/reqstars/{Id}", "GET")]
public class GetReqstar {}

[Route("/reqstars/{Id}/{Field}")]
public class ViewReqstar {}

[Route("/reqstars/{Id}/delete")]
public class DeleteReqstar {}

[Route("/reqstars/{Id}", "PATCH")]
public class UpdateReqstar {}

[Route("/reqstars/reset")]
public class ResetReqstar {}

[Route("/reqstars/search")]
[Route("/reqstars/aged/{Age}")]
public class SearchReqstars {}

These are results for these HTTP Requests 这些是这些HTTP请求的结果

GET   /reqstars           =>    AllReqstars
POST  /reqstars           =>    Reqstar
GET   /reqstars/search    =>    SearchReqstars
GET   /reqstars/reset     =>    ResetReqstar
PATCH /reqstars/reset     =>    ResetReqstar
PATCH /reqstars/1         =>    UpdateReqstar
GET   /reqstars/1         =>    GetReqstar
GET   /reqstars/1/delete  =>    DeleteReqstar
GET   /reqstars/1/foo     =>    ViewReqstar

And if there were multiple of the exact same routes declared like: 如果有多个完全相同的路由声明如下:

[Route("/req/{Id}", "GET")]
public class Req2 {}

[Route("/req/{Id}", "GET")]
public class Req1 {}

public class MyService : Service {
    public object Get(Req1 request) { ... }     
    public object Get(Req2 request) { ... }     
}

The Route on the Action that was declared first gets selected, ie: 首先声明的Action上的Route被选中,即:

GET /req/1              => Req1

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

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