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

これらの 2 つのルールは衝突しています。つまり、両方ともルートに一致します/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

したがって、これら2つのルールだけで:

.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を参照してください。

于 2013-10-16T04:34:01.997 に答える