core api

最近用core做了个小项目,在api项目中想便捷的统一返回,便写了个controller基类 

[Route("api/[controller]/[action]")]
    [ApiController]
    public class MyBaseController : ControllerBase
    {
        private ILogger<MyBaseController> _log;

        public MyBaseController(ILogger<MyBaseController> log)
        {
            _log = log;
        }

        protected ResponseResult Response(object data = null)
        {
            return new ResponseResult(data);
        }

        protected ResponseResult Response(MyException ex)
        {
            return new ResponseResult(ex);
        }

        protected ResponseResult Response(Exception ex)
        {
            var controller = ControllerContext.ActionDescriptor.ControllerName;
            var action = ControllerContext.ActionDescriptor.ActionName;
            var str = $"Controller:{controller},Action:{action},信息:";
            _log.LogError(str+ex);
            return new ResponseResult(ex);
        }
    }

在这个基类里,我对exception记录了日志,大家可根据自己的需要扩展自己想要的功能。业务的controller继承这个就可以了。上面代码中的responseresult 

public class ResponseResult:ActionResult
    {
        
        public ResponseModel Data { get; set; }

        public ResponseResult(dynamic data)
        {
            Data = new ResponseModel{ErrCode = (int)ResponseEnum.Success,Data = data};
        }

        public ResponseResult(MyException mex)
        {
            Data=new ResponseModel{ErrCode = (int)ResponseEnum.Msg,ErrMsg = mex.Message};
        }

        public ResponseResult(Exception ex)
        {
            Data=new ResponseModel{ErrCode = (int)ResponseEnum.Fail,ErrMsg = ex.Message};
        }

        public override void ExecuteResult(ActionContext context)
        {
            HttpResponse response = context.HttpContext.Response;
            response.ContentType = "application/json";
            var result = JsonConvert.SerializeObject(Data);
            response.WriteAsync(result);
        }

本来responseresult是返回的模型,但由于重写了executeresult ,要序列化返回的结果,为避免序列化自己,所以又加了返回的模型 responsemodel 

public class ResponseModel
    {
        public int ErrCode { get; set; }

        public string ErrMsg { get; set; }

        public dynamic Data { get; set; }
    }

responsemodel中data是真正返回的数据,errcode是结果标识,errmsg是信息提示。小伙伴们可根据自己的需要扩展。

业务controller中使用如下

public class OrderController : MyBaseController
    {
        private readonly IOrder _order;
        public OrderController(ILogger<MyBaseController> log, IOrder order) : base(log)
        {
            _order = order;
        }
        /// <summary>
        /// 由ID取订单详情
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Order),200)]
        public ResponseResult GetOrderById(int orderId)
        {
            try
            {
                var r = _order.GetOrder(orderId);
                return Response(r);
            }
            catch (MyException e)
            {
                return Response(e);
            }
            catch (Exception e)
            {
                return Response(e);
            }
        }
}

 

posted @ 2019-06-26 14:00  道#  阅读(1048)  评论(0编辑  收藏  举报