C# ASP.NET Web API控制器最佳实践-操作定义
我遇到的情况是,我的Web API控制器中的HttpGet操作有多种方式可以根据查询字符串中指定的参数进行调用 我需要能够处理以下GET请求:C# ASP.NET Web API控制器最佳实践-操作定义,c#,rest,asp.net-mvc-4,asp.net-web-api,.net-4.5,C#,Rest,Asp.net Mvc 4,Asp.net Web Api,.net 4.5,我遇到的情况是,我的Web API控制器中的HttpGet操作有多种方式可以根据查询字符串中指定的参数进行调用 我需要能够处理以下GET请求: ~/businesses/{businessId} ~/businesses?hasOwnProperty={propertyName} ~/businesses?latitude={lat}&longitude={long}&hasOwnProperty={propertyName} 代码示例1: [HttpGet] publi
~/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;
}
[HttpGet]
公共HttpResponseMessage获取(字符串hasOwnProperty,ODataQueryOptions查询)
{
var businessrest=\u businessRepo.get(hasOwnProperty,query);
response=Request.CreateResponse(HttpStatusCode.OK,businessresest);
response.Headers.Location=新Uri(businessresest.Href);
返回响应;
}
[HttpGet]
公共HttpResponseMessage获取(双纬度、双经度、字符串hasOwnProperty、ODataQueryOptions查询)
{
var businessresest=_businessRepo.getsbylalong(纬度、经度、hasOwnProperty、查询);
response=Request.CreateResponse(HttpStatusCode.OK,businessresest);
response.Headers.Location=新Uri(businessresest.Href);
返回响应;
}
[HttpGet]
公共HttpResponseMessageGetBusiness(字符串businessId,ODataQueryOptions查询)
{
var businessREST=_businessRepo.Get(businessId,query);
response=Request.CreateResponse(HttpStatusCode.OK,businessREST);
response.Headers.Location=新Uri(businessREST.Href);
返回响应;
}
有人建议我结合以下方法
代码示例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;
}
[HttpGet]
公共HttpResponseMessage获取(字符串businessId、双纬度、双经度、字符串hasOwnProperty、ODataQueryOptions查询)
{
如果(!String.IsNullOrEmpty(businessId))
{
//获取~/businesss/{businessId}
var businessREST=_businessRepo.Get(businessId,query);
response=Request.CreateResponse(HttpStatusCode.OK,businessREST);
response.Headers.Location=新Uri(businessREST.Href);
}
其他的
{
//GET~/Businesss?hasOwnProperty={propertyName}
//GET~/business?纬度={lat}经度={long}&hasOwnProperty={propertyName}
var businessresest=(纬度==double.MinValue | |经度==double.MinValue)
?_businessRepo.get(hasOwnProperty,query)
:_businessRepo.getsbylalong(纬度、经度、hasOwnProperty、查询);
response=Request.CreateResponse(HttpStatusCode.OK,businessresest);
response.Headers.Location=新Uri(businessresest.Href);
}
返回响应;
}
我很好奇地看到当前广泛接受的最佳做法是关于行动定义及其背后的推理的。有单独的方法会好得多,原因有以下几点: