问题描述
我有一个使用ServiceStack构建的简单REST服务。
I have a simple REST service built with ServiceStack.
如果我这样配置路由:
//register user-defined REST-ful urls
Routes
.Add<Contact>("/Contacts")
.Add<Contact>("/Contacts/{ContactId}")
此请求成功。
http://<server>:59557/Contacts?ContactId=9999 //Works
如果我这样配置路由(Business Analyst首选生成的元数据)
If I configure the routes like this (Business Analyst prefers the generated metadata)
//register user-defined REST-ful urls
Routes
.Add<UpdateContact>("/UpdateContact", "PUT")
.Add<CreateContact>("/CreateContact", "POST")
.Add<GetContact>("/Contacts/{ContactId}", "GET")
http://<server>:59557/Contacts/9999 //Works
http://<server>:59557/Contacts?ContactId=9999 //Fails, Handler for request not found
如何配置路由到第二个样本中,以便对/ Contacts?ContactId = 9999的请求成功?
How can I configure the routes in the second sample so that a request to /Contacts?ContactId=9999 will succeed?
谢谢。
推荐答案
中的某些路由在页:
[Route("/hello/{Name}")]
仅匹配:
/hello/name
其中:
[Route("/hello")]
匹配项:
/hello?Name=XXX
注意: QueryString,FormData和HTTP请求正文不是Rout的一部分e(即
并使用a具有通配符路径的路由,例如:
and using a route with a wild card path like:
[Route("/hello/{Name*}")]
匹配项:
/hello
/hello/name
/hello/my/name/is/ServiceStack
。
因此要匹配 / Customers?Key = Value
和 / Customers / {Id}
您需要为这两个路由注册匹配的路由,例如:
So to match /Customers?Key=Value
and /Customers/{Id}
you need to register matching routes for both these routes, e.g:
Routes
.Add<GetContact>("/Contacts", "GET")
.Add<GetContact>("/Contacts/{ContactId}", "GET")
如何为所有服务自动注册基于公约的路由
与此相关的还有通过扩展方法,其中仅此一次调用:
How to Auto Register Convention-based Routes for all services
Also related to this is registering Auto routes via the AddFromAssembly extension method, where this single call:
Routes.AddFromAssembly(typeof(MyService).Assembly)
浏览并扫描所有服务(在指定的程序集中),并根据以下内容注册基于约定的路由您已实现的所有HTTP方法。例如。如果您的 GetContact
和 UpdateContact
服务具有 Id
属性,它将自动注册以下路线:
Goes through and scands all your services (in the Assemblies specified) and registers convention-based routes based on all the HTTP methods you have implemented. E.g. if your GetContact
and UpdateContact
services had Id
properties it would automatically register the following routes:
Routes
.Add<GetContact>("/Contacts", "GET")
.Add<GetContact>("/Contacts/{Id}", "GET")
.Add<UpdateContact>("/Contacts", "POST PUT")
.Add<UpdateContact>("/Contacts/{Id}", "POST PUT")
仅有一个 Contacts
REST Service及其所有HTTP动词的实现,它将注册以下路由:
And if you just had a single Contacts
REST Service with implementations for all the HTTP Verbs it would register these routes:
Routes
.Add<Contacts>("/Contacts", "GET POST PUT DELETE PATCH")
.Add<Contacts>("/Contacts/{Id}", "GET POST PUT DELETE PATCH")
路由解决订单
这在,但用于选择路线的权重基于:
Routing Resolution Order
This is described in more detail on the New API Design wiki but the weighting used to select a route is based on:
- 首先使用任何精确的文字匹配
- 完全动词匹配优于所有动词
- 路线中的变量越多,权重就越小
- 如果路线的权重相同,则顺序由操作在服务或注册顺序(FIFO)中的位置决定。
- Any exact Literal Matches are used first
- Exact Verb match is preferred over All Verbs
- The more variables in your route the less weighting it has
- When Routes have the same weight, the order is determined by the position of the Action in the service or Order of Registration (FIFO)
有关示例,请参见Wiki上的部分。
See the Smart Routing section on the wiki for examples.
由于,我认为值得指出的是ServiceStack的路由实现是通过哈希查找和因此不会受到线性性能的影响ce您可能曾经在MVC中遇到过回归问题。
Since Routing in MVC can be slow when you have a large number of Routes, I think it's worthwhile pointing out ServiceStack's Routing implementation is implemented with hash lookups and so doesn't suffer the linear performance regression issues you might have had with MVC.
这篇关于ServiceStack路由不适用于querystring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!