单独或合并的 ServiceStack 服务? [英] Separate or combined ServiceStack services?
问题描述
我想要像下面这样的 ServiceStack 端点...
I want to have ServiceStack endpoints such as the following...
[RestService("/items/recent")]
[RestService("/items/recent/{Page}")]
[RestService("/items/popular")]
[RestService("/items/popular/{Page}")]
因为两者都会返回一个 List
,我希望能够在同一个 RestServiceBase
中包含这两个,以便于代码管理.这可能吗?如果是这样,我如何在请求进来时区分它是最近的"还是流行的"请求,以便我可以适当地处理它?</p>
Since both would return a List<Item>
, I'd love to be able to have both of these in the same RestServiceBase
for easier code management. Is this possible? If so, how can I differentiate the request when it comes in to find whether it was a "recent" or "popular" request so that I can handle it appropriately?
推荐答案
是的,您可以让多个 REST-ful 路径指向同一个 Web 服务.
Yes you can have multiple REST-ful paths pointing to the same web service.
如果您想保持路径不变,您可以检查用于通过 HttpRequest 调用服务的请求路径,即:
If you want to leave the paths as-is you can inspect the Request Path used to invoke the service via the HttpRequest, i.e:
var httpReq = base.RequestContext.Get<IHttpRequest>();
httpReq.PathInfo //or httpReq.RawUrl, httpReq.AbsoluteUri, etc.
确定请求类型的方法是查看填充的请求 DTO - 但要区分 /recent/ 和 /popular/应该将它存储在另一个请求 DTO 属性中,然后检查它的值,即
The way you work out what type of request it is, is by looking at the populated Request DTO - but to distinguish between /recent/ and /popular/ you should store it in another Request DTO property and then inspect its values i.e.
[RestService("/items/{Type}")]
[RestService("/items/{Type}/{Page}")]
public class Items
{
public string Type { get; set; }
public int? Page { get; set; }
}
public class ItemsService : ServiceBase<Items>
{
public override object Run(Items request)
{
if (request.Type == "recent")
if (!request.Page.HasValue)
//path 1
else
//path 2
else if (request.Type == "popular")
if (!request.Page.HasValue)
//path 3
else
//path 4
}
}
这也类似于这个 StackOverflow 问题:需要服务堆栈实现方面的帮助
This is also similar to this StackOverflow question: Need help on servicestack implementation
这篇关于单独或合并的 ServiceStack 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!