增加webapi 错误统一返回
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using NewLife.Log;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Web.Controllers.api;
|
||||
|
||||
namespace COMCMS.Web.Common
|
||||
{
|
||||
public class WebAPIHandleErrorAttribute : ExceptionFilterAttribute
|
||||
{
|
||||
public override void OnException(ExceptionContext context)
|
||||
{
|
||||
context.HttpContext.Request.ContentType = "application/json";
|
||||
context.HttpContext.Request.Headers["Accept"] = "application/json";
|
||||
ReJSON model = new ReJSON();
|
||||
model.code = 5001;
|
||||
model.message = "执行错误:"+ context.Exception.Message;
|
||||
context.Result = new JsonResult(model);
|
||||
base.OnException(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Web.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -10,6 +11,7 @@ namespace COMCMS.Web.Controllers.api
|
||||
{
|
||||
[Produces("application/json")]
|
||||
[Route("api/[controller]/[action]")]
|
||||
[WebAPIHandleError]
|
||||
public class APIBaseController : Controller
|
||||
{
|
||||
#region 通用信息
|
||||
|
||||
@@ -47,5 +47,15 @@ namespace COMCMS.Web.Controllers.api
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 测试出错
|
||||
[HttpGet]
|
||||
public object GetTest()
|
||||
{
|
||||
int a = 1,b=0;
|
||||
var c = a / b;
|
||||
return reJson;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user