我想在我的应用中定义以下路线:
/s/customers/1234/summary
和
/s/locations/5767/summary
现在通常我会像这样定义我的路线:
Add<CustomerSummaryRequest>("/s/customers/{Id}/summary")
Add<LocationSummaryRequest>("/s/locations/{Id}/summary")
但问题是,我的数据库中客户和位置的ID本身是
/customers/1234
/locations/5767
并且最终会喜欢这些路线:
Add<CustomerSummaryRequest>("/s/{CustomerId*}/summary")
Add<LocationSummaryRequest>("/s/{LocationId*}/summary")
有什么建议吗?
我意识到我能做到:
Add<CustomerSummaryRequest>("/s/Customers/{CustomerId*}/summary")
Add<LocationSummaryRequest>("/s/Locations/{LocationId*}/summary")
这将给我id的数字部分。然后我可以将id与customers /或/ locations /
组合在一起答案 0 :(得分:3)
您不能在ServiceStack中拥有不明确的路由,因此您可以拥有一个捕获所有请求来处理请求和路由到相应的服务,例如:
[Route("/s/{Id*}/summary")]
public class SummaryRequest
{
public string Id { get; set; }
}
public object Any(SummaryRequest request)
{
var id = "/" + request.Id;
return request.Id.StartsWith("customers/")
? Gateway.Send(new CustomerSummaryRequest { CustomerId = id })
: Gateway.Send(new LocationSummaryRequest { LocationId = id });
}
或者您有不同服务的独特路线,例如:
[Route("/s/customers/{CustomerId}/summary")]
public class CustomerSummaryRequest
{
public int CustomerId { get; set; }
}
[Route("/s/locations/{LocationId}/summary")]
public class CustomerSummaryRequest
{
public int LocationId { get; set; }
}
在您的服务中构建您的ID。