C# 封装WEB API Rest结果
我正在构建一个WEB API,它有许多GET方法,如下所示:C# 封装WEB API Rest结果,c#,rest,asp.net-web-api,C#,Rest,Asp.net Web Api,我正在构建一个WEB API,它有许多GET方法,如下所示: [HttpGet] [Authorize] [Route("monedas")] public IHttpActionResult GetMonedas(string empresaId, string filtro = "") { IEnumeradorService sectoresService = new MonedasServic
[HttpGet]
[Authorize]
[Route("monedas")]
public IHttpActionResult GetMonedas(string empresaId, string filtro = "")
{
IEnumeradorService sectoresService = new MonedasService(empresaId);
if (!initialValidation.EmpresaPerteneceACliente(empresaId, User))
{
return Content(HttpStatusCode.MethodNotAllowed, "La empresa no existe o el cliente no tiene acceso a ella");
}
try
{
return Ok(sectoresService.Enumerar(filtro));
}
catch (QueryFormatException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
return InternalServerError();
}
}
[HttpGet]
[Authorize]
[Route("paises")]
public IHttpActionResult GetPaises(string empresaId, string filtro = "")
{
IEnumeradorService sectoresService = new PaisesService(empresaId);
if (!initialValidation.EmpresaPerteneceACliente(empresaId, User))
{
return Content(HttpStatusCode.MethodNotAllowed, "La empresa no existe o el cliente no tiene acceso a ella");
}
try
{
return Ok(sectoresService.Enumerar(filtro));
}
catch (QueryFormatException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
return InternalServerError();
}
}
如何用可重用的代码封装此类行为?您可以通过创建ExceptionFilterAttribute删除所有try/catch语句: 然后将其添加到应用程序中:
config.Filters.Add(new HandleExceptionsFilter());
这将使您的操作如下所示:
[HttpGet]
[Authorize]
[Route("monedas")]
public IHttpActionResult GetMonedas(string empresaId, string filtro = "")
{
IEnumeradorService sectoresService = new MonedasService(empresaId);
if (!initialValidation.EmpresaPerteneceACliente(empresaId, User))
{
return Content(HttpStatusCode.MethodNotAllowed, "La empresa no existe o el cliente no tiene acceso a ella");
}
return Ok(sectoresService.Enumerar(filtro));
}
[HttpGet]
[Authorize]
[Route("paises")]
public IHttpActionResult GetPaises(string empresaId, string filtro = "")
{
IEnumeradorService sectoresService = new PaisesService(empresaId);
if (!initialValidation.EmpresaPerteneceACliente(empresaId, User))
{
return Content(HttpStatusCode.MethodNotAllowed, "La empresa no existe o el cliente no tiene acceso a ella");
}
return Ok(sectoresService.Enumerar(filtro));
}
谢谢你的时间!
[HttpGet]
[Authorize]
[Route("monedas")]
public IHttpActionResult GetMonedas(string empresaId, string filtro = "")
{
IEnumeradorService sectoresService = new MonedasService(empresaId);
if (!initialValidation.EmpresaPerteneceACliente(empresaId, User))
{
return Content(HttpStatusCode.MethodNotAllowed, "La empresa no existe o el cliente no tiene acceso a ella");
}
return Ok(sectoresService.Enumerar(filtro));
}
[HttpGet]
[Authorize]
[Route("paises")]
public IHttpActionResult GetPaises(string empresaId, string filtro = "")
{
IEnumeradorService sectoresService = new PaisesService(empresaId);
if (!initialValidation.EmpresaPerteneceACliente(empresaId, User))
{
return Content(HttpStatusCode.MethodNotAllowed, "La empresa no existe o el cliente no tiene acceso a ella");
}
return Ok(sectoresService.Enumerar(filtro));
}