我有一个使用 ServiceStack 构建的简单 REST 服务。
如果我这样配置路由:
//register user-defined REST-ful urls
Routes
.Add<Contact>("/Contacts")
.Add<Contact>("/Contacts/{ContactId}")
此请求成功。
http://<server>:59557/Contacts?ContactId=9999 //Works
如果我像这样配置路由(业务分析师更喜欢生成的元数据)
//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 的请求成功?
谢谢。
最佳答案
ServiceStack中的一些路由在 Your first website explained wiki 上进行了解释页:
[Route("/hello/{Name}")]
仅匹配:
/hello/name
然而:
[Route("/hello")]
火柴:
/hello?Name=XXX
注意:QueryString、FormData 和 HTTP 请求正文不是路由的一部分(即只有/path/info 是),但除了每个 Web 服务调用之外,它们都可以用于进一步填充请求 DTO。
并使用带有通配符路径的路由,例如:
[Route("/hello/{Name*}")]
火柴:
/hello
/hello/name
/hello/my/name/is/ServiceStack
Another good use-case for when to use wildcard routes .
所以要匹配
/Customers?Key=Value
和 /Customers/{Id}
您需要为这两条路线注册匹配的路线,例如:Routes
.Add<GetContact>("/Contacts", "GET")
.Add<GetContact>("/Contacts/{ContactId}", "GET")
如何为所有服务自动注册基于约定的路线
与此相关的还有通过AddFromAssembly 注册自动路线。扩展方法,其中这个单一的调用:
Routes.AddFromAssembly(typeof(MyService).Assembly)
遍历并扫描您的所有服务(在指定的程序集中)并根据您已实现的所有 HTTP 方法注册基于约定的路由。例如。如果您的
GetContact
和 UpdateContact
服务有Id
属性它将自动注册以下路由:Routes
.Add<GetContact>("/Contacts", "GET")
.Add<GetContact>("/Contacts/{Id}", "GET")
.Add<UpdateContact>("/Contacts", "POST PUT")
.Add<UpdateContact>("/Contacts/{Id}", "POST PUT")
如果你只有一个
Contacts
具有所有 HTTP 动词实现的 REST 服务,它将注册这些路由:Routes
.Add<Contacts>("/Contacts", "GET POST PUT DELETE PATCH")
.Add<Contacts>("/Contacts/{Id}", "GET POST PUT DELETE PATCH")
路由解析顺序
这在 New API Design wiki 上有更详细的描述。但用于选择路线的权重基于:
见Smart Routing wiki 上的部分以获取示例。
很棒的演出
从 Routing in MVC can be slow当您有大量路由时,我认为值得指出 ServiceStack 的路由实现是通过哈希查找实现的,因此不会遭受 MVC 可能遇到的线性性能回归问题。
关于routing - ServiceStack 路由不适用于查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164201/