Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 封装WEB API Rest结果_C#_Rest_Asp.net Web Api - Fatal编程技术网

C# 封装WEB API Rest结果

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

我正在构建一个WEB API,它有许多GET方法,如下所示:

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