3

私はこの短いテストコードを実行しました。ただし、他のすべてのルートを無視し、最初のルートにのみヒットします。

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;
    }
}
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 に答える