ServiceStack多路由路径
本文关键字:路径 路由 多路 ServiceStack | 更新日期: 2023-09-27 18:02:49
我已经完成了这个简短的测试代码。但是,它忽略所有其他路由,只访问第一个路由:
http://localhost:55109/api/customers
工作正常
http://localhost:55109/api/customers/page/1
不工作
http://localhost:55109/api/customers/page/1/size/20
不工作
当我用page &size参数显示:"Handler for Request not found".
[Route("/api/customers", "GET")] //works okay
[Route("/api/customers/page/{Page}", "GET")] //doesn't work
[Route("/api/customers/page/{Page}/size/{PageSize}", "GET")] //doesn't work
public class Customers {
public Customers() { Page = 1; PageSize = 20; } //by default 1st page 20 records
public int Page { get; set; }
public int PageSize { get; set; }
}
//----------------------------------------------------
public class CustomersService : Service {
public ICustomersManager CustomersManager { get; set; }
public dynamic Get(Customers req) {
return new { Customers = CustomersManager.GetCustomers(req) };
}
}
//----------------------------------------------------
public interface ICustomersManager : IBaseManager {
IList<Customer> GetCustomers(Customers req);
}
public class CustomersManager : BaseManager, ICustomersManager {
public IList<Customer> GetCustomers(Customers req) {
if (req.Page < 1) ThrowHttpError(HttpStatusCode.BadRequest, "Bad page number");
if (req.PageSize < 1) ThrowHttpError(HttpStatusCode.BadRequest, "Bad page size number");
var customers = Db.Select<Customer>().Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
if (customers.Count <= 0) ThrowHttpError(HttpStatusCode.NotFound, "Data not found");
return customers;
}
}
你不应该用/api
作为你所有路由的前缀,这看起来应该是ServiceStack应该挂载的自定义路径,而不是单个服务。
我不确定我能提供一个解决方案,但也许是一个提示。我没有看到你的路由路径有任何不正确的地方(我同意@mythz关于删除/api
和使用自定义路径的说法),我能够得到一个类似的路由路径结构正确工作。在您的CustomersService
类中,我剥离了一些代码,以获得一个更简单的调试示例。我还在返回中添加了一个Paths
属性,只是为了查看在您对/api/customers
的请求中没有看到/api/customers/page/{Page}
或/api/customers/page/{Page}/size/{PageSize}
的机会中注册了哪些路径。希望对你有帮助。
[Route("/api/customers", "GET")] //works okay
[Route("/api/customers/page/{Page}", "GET")] //doesn't work
[Route("/api/customers/page/{Page}/size/{PageSize}", "GET")] //doesn't work
public class Customers
{
public Customers() { Page = 1; PageSize = 20; } //by default 1st page 20 records
public int Page { get; set; }
public int PageSize { get; set; }
}
//----------------------------------------------------
public class CustomersService : Service
{
public dynamic Get(Customers req)
{
var paths = ((ServiceController) base.GetAppHost().Config.ServiceController).RestPathMap.Values.SelectMany(x => x.Select(y => y.Path)); //find all route paths
var list = String.Join(", ", paths);
return new { Page = req.Page, PageSize = req.PageSize, Paths = list };
}
}