/v1/Tenants/{TenantId}
路由正常,但/v1/Tenants/{TenantName}
路由不起作用,不确定是什么问题?此外,这是设计或具有过滤条件的正确方法吗?
//Request DTO
[Route("/v1/Tenants/{TenantName}", "GET")]
public class TenantRequestByTenantName: IReturn<TenantResponse>
{
public string TenantName { get; set; }
}
//Request DTO
[Route("/v1/Tenants/{TenantId}", "GET")]
public class TenantRequestById : IReturn<TenantResponse>
{
public int? TenantId { get; set; }
}
//Response DTO
public class TenantResponse
{
public ITenantEntity Result { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class RestaurantService : Service
{
public object Any(TenantRequestById request)
{
return this.GetTenant(request);
}
public object Any(TenantRequestByTenantName request)
{
return this.GetTenant(request);
}
private object GetTenant(object whereConditions)
{
return new TenantResponse
{
Result = new TenantManager().GetRow(whereConditions)
};
}
}
答案 0 :(得分:2)
路线不明确,因为两条路线都处理相同的路线,即:
/v1/Tenants/xxx
您需要拥有单独的路线,例如:
[Route("/v1/tenants/{TenantId}")]
public class TenantRequestById { ... }
[Route("/v1/tenants/by-name/{TenantName}")]
public class TenantRequestByTenantName { ... }
或者您可以使用单个服务来处理这两个请求,具体取决于它是否为整数as done in TechStacks:
[Route("/technology/{Slug}")]
public class GetTechnology : IReturn<GetTechnologyResponse>
{
public string Slug { get; set; }
public long Id
{
set { this.Slug = value.ToString(); }
}
}
Id
重载允许您键入的客户端拥有理想的API,例如:
var response = client.Get(GetTechnology { Id = 1 });
var response = client.Get(GetTechnology { Slug = "servicestack" });
然后在您的服务实现中,您可以检查Slug是否为整数ID,以选择适当的查询,例如:
public object Get(GetTechnology request)
{
int id;
var tech = int.TryParse(request.Slug, out id)
? Db.SingleById<Technology>(id)
: Db.Single<Technology>(x => x.Slug == request.Slug.ToLower());
return new GetTechnologyResponse {
Result = tech,
};
}