[英]ServiceStack multiple routing paths
我做了這個簡短的測試代碼。 但是,它會忽略所有其他路線,只會觸及第一條路線:
http://localhost:55109/api/customers
工作正常
http://localhost:55109/api/customers/page/1
不起作用
http://localhost:55109/api/customers/page/1/size/20
將無法正常工作
當我使用頁面和大小參數調用路徑時,它會顯示: "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/page/{Page}
或/api/customers/page/{Page}/size/{PageSize}
您對/api/customers
請求中的/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 };
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.