api增加全局异常过滤器
This commit is contained in:
Binary file not shown.
@@ -20,6 +20,8 @@ namespace WebAPI
|
|||||||
config.MapHttpAttributeRoutes();
|
config.MapHttpAttributeRoutes();
|
||||||
//
|
//
|
||||||
config.Filters.Add(new TestPermissionAttribute());
|
config.Filters.Add(new TestPermissionAttribute());
|
||||||
|
config.Filters.Add(new GlobalExceptionFilter());
|
||||||
|
|
||||||
config.Routes.MapHttpRoute(
|
config.Routes.MapHttpRoute(
|
||||||
name: "DefaultApi",
|
name: "DefaultApi",
|
||||||
routeTemplate: "api/{controller}/{action}/{id}",
|
routeTemplate: "api/{controller}/{action}/{id}",
|
||||||
|
|||||||
@@ -167,6 +167,6 @@ namespace WebAPI.Controllers
|
|||||||
|
|
||||||
return responeData;
|
return responeData;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,11 +133,9 @@ namespace WebAPI.Controllers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
returnData.data = await HSSEData_HSSEService.GetTodayHSSEData_HSSE();
|
returnData.data = await HSSEData_HSSEService.GetTodayHSSEData_HSSE();
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
||||||
returnData.code = 0;
|
returnData.code = 0;
|
||||||
returnData.message = ex.ToString();
|
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\ToDoItemController.cs" />
|
||||||
<Compile Include="Controllers\UnitController.cs" />
|
<Compile Include="Controllers\UnitController.cs" />
|
||||||
<Compile Include="Controllers\UserController.cs" />
|
<Compile Include="Controllers\UserController.cs" />
|
||||||
|
<Compile Include="Filter\GlobalExceptionFilter.cs" />
|
||||||
<Compile Include="Filter\TestPermissionAttribute.cs" />
|
<Compile Include="Filter\TestPermissionAttribute.cs" />
|
||||||
<Compile Include="Global.asax.cs">
|
<Compile Include="Global.asax.cs">
|
||||||
<DependentUpon>Global.asax</DependentUpon>
|
<DependentUpon>Global.asax</DependentUpon>
|
||||||
|
|||||||
Reference in New Issue
Block a user