2013-03-07 235 views
3

我已經完成了這個簡短的測試代碼。但是,它忽略了所有其它的途徑,只打了第一條路線:ServiceStack多個路由路徑

http://localhost:55109/api/customers工作好

http://localhost:55109/api/customers/page/1將無法​​正常工作

http://localhost:55109/api/customers/page/1/size/20將無法​​正常工作

當我打電話與頁面的路徑& size params它說:"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; 
    } 
} 

回答

2

不知道我可以提供一個解決方案,但也許是一個提示。我在路徑路徑中沒有看到任何不正確的地方(我同意@mythz關於刪除/api和使用自定義路徑的說法),並且我能夠得到類似的路徑路徑結構正常工作。在您的CustomersService類中,我剝離了一些代碼以獲得用於調試的簡單示例。我還在回報中添加了Paths屬性,以查看哪些路徑註冊的機會在或/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 }; 
    } 
} 
+0

感謝關於顯示路徑路徑的提示,這很有用。 – Tom 2013-03-07 22:58:13