简体   繁体   English

ServiceStack多个路由路径

[英]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工作正常

http://localhost:55109/api/customers/page/1 won't work http://localhost:55109/api/customers/page/1不起作用

http://localhost:55109/api/customers/page/1/size/20 won't work http://localhost:55109/api/customers/page/1/size/20将无法正常工作

When I call the routes with page & size params it says: "Handler for Request not found". 当我使用页面和大小参数调用路径时,它会显示: "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. 我在路径路径中没有看到任何不正确的内容(我同意@mythz关于删除/api和使用自定义路径的说法)并且我能够使类似的路径路径结构正常工作。 In your CustomersService class I stripped out some code to get to a simplier example for debugging. 在您的CustomersService类中,我删除了一些代码以获得更简单的调试示例。 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 . 我还在返回时添加了一个Paths属性,以查看在没有看到的情况下注册了哪些路径/api/customers/page/{Page}/api/customers/page/{Page}/size/{PageSize}您对/api/customers请求中的/api/customers/page/{Page}/size/{PageSize} 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 };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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