3

我已经完成了这个简短的测试代码。但是,它会忽略所有其他路由,只命中第一条路由:

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;
    }
}
4

2 回答 2

2

您不应该为所有路由添加前缀/api,这看起来应该是ServiceStack 应该安装在的自定义路径,而不是单个服务。

于 2013-03-07T15:12:49.620 回答
2

不确定我可以提供解决方案,但也许是一个提示。我在您的路由路径中看不到任何不正确的地方(我同意@mythz 关于删除/api和使用自定义路径的说法),并且我能够使类似的路由路径结构正常工作。在您的CustomersService课程中,我删除了一些代码以获得更简单的调试示例。我还在返回时添加了一个属性,只是为了查看在您看不到的情况下或在您的请求Paths中注册了哪些路径。希望这可以帮助。/api/customers/page/{Page}/api/customers/page/{Page}/size/{PageSize}/api/customers

[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 };
    }
}
于 2013-03-07T17:10:57.000 回答