ServiceStack.net - 搜索路线?

时间:2013-10-16 04:26:11

标签: servicestack

举一个基于不同标准搜索Customer实体的简单示例:

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

这似乎不起作用。如何定义单独的路由以在不同的字段上启用搜索条件?

1 个答案:

答案 0 :(得分:3)

这两条规则发生冲突,即它们都匹配路线/customers/x

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

默认情况下此规则:

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

还允许您使用QueryString填充Request DTO,例如:

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

只有这两条规则:

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

让您查询:

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

如果您希望使用/ pathinfo访问LastName,则需要使用非冲突路由,例如:

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

有关详细信息,请参阅Routing wiki