C# ServiceStack多个路由路径
我已经完成了这个简短的测试代码。但是,它会忽略所有其他路线,只会命中第一条路线:C# ServiceStack多个路由路径,c#,
servicestack,C#,
servicestack,我已经完成了这个简短的测试代码。但是,它会忽略所有其他路线,只会命中第一条路线: http://localhost:55109/api/customers工作正常 http://localhost:55109/api/customers/page/1不起作用 http://localhost:55109/api/customers/page/1/size/20不起作用 当我使用page&size参数调用路由时,它会显示:“未找到请求的处理程序”。 我不知道我做错了什么?请给我一个提示 [Rout
http://localhost:55109/api/customers
工作正常
http://localhost:55109/api/customers/page/1
不起作用
http://localhost:55109/api/customers/page/1/size/20
不起作用
当我使用page&size参数调用路由时,它会显示:“未找到请求的处理程序”。
我不知道我做错了什么?请给我一个提示
[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;
}
}
[Route(“/api/customers”,“GET”)]//工作正常
[Route(“/api/customers/page/{page}”,“GET”)]//不起作用
[Route(“/api/customers/page/{page}/size/{PageSize}”,“GET”)]//不起作用
公共类客户{
公共客户(){Page=1;PageSize=20;}//默认情况下第1页为20条记录
公共整型页{get;set;}
公共int PageSize{get;set;}
}
//----------------------------------------------------
公共类CustomerService:服务{
公共ICCustomersManager客户管理器{get;set;}
公共动态获取(客户请求){
返回新的{Customers=CustomersManager.GetCustomers(req)};
}
}
//----------------------------------------------------
公共接口ICCustomersManager:IBaseManager{
IList GetCustomers(客户需求);
}
公共类CustomerManager:BaseManager、ICCustomersManager{
公共IList GetCustomers(客户需求){
如果(请求页<1)通过HttpPeror(HttpStatusCode.BadRequest,“错误页码”);
如果(req.PageSize<1)通过HttpPeror(HttpStatusCode.BadRequest,“错误页面大小编号”);
var customers=Db.Select().Skip((req.Page-1)*req.PageSize).Take(req.PageSize).ToList();
如果(customers.Count您不应该在所有路线前加上/api
,这看起来应该是服务,而不是单独的服务。不确定我能否提供一个解决方案,但可能是一个提示。我没有看到您的路线路径中有任何错误(我同意@mythz关于删除/api
和使用自定义路径的说法)并且我能够得到一个类似的路由路径结构正常工作。在您的CustomersService
类中,我去掉了一些代码,以获得一个更简单的调试示例。我还在返回中添加了一个path
属性,只是为了查看哪些路径在您可能看不到的情况下注册了/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 };
}
}
我做了两个“谢谢你关于显示路径的提示。这很有用。”。