api增加全局异常过滤器
This commit is contained in:
parent
f4fb0df3e9
commit
bc45dce29e
Binary file not shown.
|
|
@ -20,6 +20,8 @@ namespace WebAPI
|
|||
config.MapHttpAttributeRoutes();
|
||||
//
|
||||
config.Filters.Add(new TestPermissionAttribute());
|
||||
config.Filters.Add(new GlobalExceptionFilter());
|
||||
|
||||
config.Routes.MapHttpRoute(
|
||||
name: "DefaultApi",
|
||||
routeTemplate: "api/{controller}/{action}/{id}",
|
||||
|
|
|
|||
|
|
@ -167,6 +167,6 @@ namespace WebAPI.Controllers
|
|||
|
||||
return responeData;
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -133,11 +133,9 @@ namespace WebAPI.Controllers
|
|||
try
|
||||
{
|
||||
returnData.data = await HSSEData_HSSEService.GetTodayHSSEData_HSSE();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
returnData.code = 0;
|
||||
returnData.message = ex.ToString();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http.Filters;
|
||||
using System.Web.Http;
|
||||
using BLL;
|
||||
|
||||
namespace WebAPI.Filter
|
||||
{
|
||||
public class GlobalExceptionFilter : ExceptionFilterAttribute
|
||||
{
|
||||
public override void OnException(HttpActionExecutedContext context)
|
||||
{
|
||||
// Log the exception here if needed
|
||||
ErrLogInfo.WriteLog(context.Exception, "WebApi", context.Request.RequestUri.ToString());
|
||||
context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, new
|
||||
{
|
||||
Message = "An error occurred.",
|
||||
DetailedMessage = context.Exception.Message
|
||||
});
|
||||
|
||||
base.OnException(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -243,6 +243,7 @@
|
|||
<Compile Include="Controllers\ToDoItemController.cs" />
|
||||
<Compile Include="Controllers\UnitController.cs" />
|
||||
<Compile Include="Controllers\UserController.cs" />
|
||||
<Compile Include="Filter\GlobalExceptionFilter.cs" />
|
||||
<Compile Include="Filter\TestPermissionAttribute.cs" />
|
||||
<Compile Include="Global.asax.cs">
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
|
|
|
|||
Loading…
Reference in New Issue