首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >封装WEB结果

封装WEB结果
EN

Stack Overflow用户
提问于 2017-10-09 22:52:28
回答 1查看 309关注 0票数 1

我正在构建一个WEB,它有很多这样的GET方法:

代码语言:javascript
复制
    [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();
        }
    }

如何用可重用的代码封装这种行为?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-11 14:52:27

可以通过创建一个ExceptionFilterAttribute来删除所有try/catch语句

代码语言:javascript
复制
public class HandleExceptionsFilter : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        if (context.Exception is QueryFormatException)
        {
            context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, context.Exception.Message);
            return;
        }

        System.Diagnostics.Debug.WriteLine(context.Exception);
        context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }
}

然后将其添加到应用程序中:

代码语言:javascript
复制
config.Filters.Add(new HandleExceptionsFilter());

这将使您的行为看起来像这样:

代码语言:javascript
复制
[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));
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46656156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档