【问题标题】:ServiceStack multiple routing pathsServiceStack 多路由路径
【发布时间】:2013-03-08 01:10:57
【问题描述】:

我已经完成了这个简短的测试代码。但是,它会忽略所有其他路线,只命中第一条路线:

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

【问题讨论】:

    标签: c# servicestack


    【解决方案1】:

    您不应该在所有路由前加上 /api,这看起来应该是 custom path where ServiceStack should be mounted at,而不是个别服务。

    【讨论】:

    • 我同时做了 "
    【解决方案2】:

    不确定我能否提供解决方案,但也许可以提供一个提示。我在您的路由路径中看不到任何不正确的地方(我同意 @mythz 关于删除 /api 并使用自定义路径的说法)并且我能够使类似的路由路径结构正常工作。在您的CustomersService 类中,我删除了一些代码以获得更简单的调试示例。我还在返回上添加了Paths 属性,只是为了查看在您对/api/customers 的请求中看不到/api/customers/page/{Page}/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 };
        }
    }
    

    【讨论】:

    • 感谢有关显示路线路径的提示。很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多