Web API コントローラーの HttpGet アクションが、クエリ文字列で指定されたパラメーターに基づいて複数の方法で呼び出される状況に陥っています。
次の GET リクエストを処理できる必要があります。
~/businesses/{businessId}
~/businesses?hasOwnProperty={propertyName}
~/businesses?latitude={lat}&longitude={long}&hasOwnProperty={propertyName}
コード例 1:
[HttpGet]
public HttpResponseMessage Get(string hasOwnProperty, ODataQueryOptions<Core.Models.Entities.Business> query)
{
var businessesREST = _businessRepo.Gets(hasOwnProperty, query);
response = Request.CreateResponse(HttpStatusCode.OK, businessesREST);
response.Headers.Location = new Uri(businessesREST.Href);
return response;
}
[HttpGet]
public HttpResponseMessage Get(double latitude, double longitude, string hasOwnProperty, ODataQueryOptions<Core.Models.Entities.Business> query)
{
var businessesREST = _businessRepo.GetsByLatLong(latitude, longitude, hasOwnProperty, query);
response = Request.CreateResponse(HttpStatusCode.OK, businessesREST);
response.Headers.Location = new Uri(businessesREST.Href);
return response;
}
[HttpGet]
public HttpResponseMessage GetBusiness(string businessId, ODataQueryOptions<Core.Models.Entities.Business> query)
{
var businessREST = _businessRepo.Get(businessId, query);
response = Request.CreateResponse(HttpStatusCode.OK, businessREST);
response.Headers.Location = new Uri(businessREST.Href);
return response;
}
以下の方法を組み合わせることが提案されています。
コード例 2:
[HttpGet]
public HttpResponseMessage Get(string businessId, double latitude, double longitude, string hasOwnProperty, ODataQueryOptions<Core.Models.Entities.Business> query)
{
if (!String.IsNullOrEmpty(businessId))
{
//GET ~/businesses/{businessId}
var businessREST = _businessRepo.Get(businessId, query);
response = Request.CreateResponse(HttpStatusCode.OK, businessREST);
response.Headers.Location = new Uri(businessREST.Href);
}
else
{
//GET ~/businesses?hasOwnProperty={propertyName}
//GET ~/businesses?latitude={lat}&longitude={long}&hasOwnProperty={propertyName}
var businessesREST = (latitude == double.MinValue || longitude == double.MinValue)
? _businessRepo.Gets(hasOwnProperty, query)
: _businessRepo.GetsByLatLong(latitude, longitude, hasOwnProperty, query);
response = Request.CreateResponse(HttpStatusCode.OK, businessesREST);
response.Headers.Location = new Uri(businessesREST.Href);
}
return response;
}
アクション定義とその背後にある理由に関して、現在広く受け入れられているベスト プラクティスが何であるかを知りたいと思っています。