我已经完成了这段简短的测试代码.但是,它会忽略所有其他路线,只会到达第一个路线:
http:// localhost:55109 / api / customers工作正常
http:// localhost:55109 / api / customers / page / 1无法正常工作
http:// localhost:55109 / api / customers / page / 1 / size / 20无法正常工作
当我用&页面呼叫路线时大小参数表示:“找不到请求处理程序”.
我不知道自己做错了什么?请给我提示吗?
[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前缀,这看起来应该是custom path where ServiceStack should be mounted at,而不是单独的服务.