2013-10-16 30 views
1

就拿簡單的例子,搜索基於不同的標準一Customer實體:ServiceStack.net - 搜索路線?

public class Customer : IReturn<CustomerDTO> 
{ 
    public int Id { get; set; } 
    public string LastName { get; set; } 
} 

public class CustomerDTO 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string ZipCode { get; set; } 
} 

然後我有以下的路線設置:

public override void Configure(Funq.Container container) 
{ 
    Routes 
     .Add<Customer>("/customers", "GET") 
     .Add<Customer>("/customers/{Id}", "GET") 
     .Add<Customer>("/customers/{LastName}", "GET"); 
} 

這似乎並沒有工作。如何定義單獨的路線以在不同的字段上啓用搜索條件?

回答

3

這條規則的衝突,即它們都匹配的路由/customers/x

.Add<Customer>("/customers/{Id}", "GET") 
.Add<Customer>("/customers/{LastName}", "GET"); 

默認情況下此規則:

.Add<Customer>("/customers", "GET") 

而且您可以填充使用的查詢字符串,例如請求DTO:

/customers?Id=1 
/customers?LastName=foo 

所以只有這條規則:

.Add<Customer>("/customers", "GET") 
.Add<Customer>("/customers/{Id}", "GET") 

讓您與查詢:

/customers 
/customers/1 
/customers?LastName=foo 

如果你想與姓氏訪問的/ PATHINFO你需要使用非撞擊的路線,如:

.Add<Customer>("/customers/by-name/{LastName}", "GET") 

欲瞭解更多信息,請參閱Routing wiki

+0

那麼查詢參數是否區分大小寫?他們必須匹配請求對象上的屬性大小寫? – Sam

+1

@Sam屬性不區分大小寫。 – mythz