简体   繁体   中英

ServiceStack multiple routing paths

I have done this short testing code. However, it ignores all other routes and only hits the first route:

http://localhost:55109/api/customers works okay

http://localhost:55109/api/customers/page/1 won't work

http://localhost:55109/api/customers/page/1/size/20 won't work

When I call the routes with page & size params it says: "Handler for Request not found".

I can't figure out what I have done wrong? Please throw me a hint?

[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自定义路径 ,而不是单个服务。

Not sure I can offer a solution but maybe a hint. I don't see anything incorrect in your route paths (I do agree with what @mythz says about removing /api and using custom path) and I am able to get a similar route path structure working correctly. In your CustomersService class I stripped out some code to get to a simplier example for debugging. I also added a Paths property on the return just to see what paths are registered in the off chance you don't see /api/customers/page/{Page} or /api/customers/page/{Page}/size/{PageSize} in your request to /api/customers . Hope this helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM