1

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

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

4

1 回答 1

3

这两个规则是冲突的,即它们都匹配路由/customers/x

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

默认情况下,此规则:

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

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

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

所以只有这两条规则:

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

让您查询:

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

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

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

有关详细信息,请参阅路由 wiki

于 2013-10-16T04:34:01.997 回答