Làm thế nào để đăng ký bộ lọc ngoại lệ trên toàn cầu?
Làm thế nào để đăng ký bộ lọc ngoại lệ trên toàn cầu?
Đăng ký bộ lọc ngoại lệ toàn cục:
public class ApiExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext ctx) {
var (status, msg) = ctx.Exception switch { NotFoundException => (HttpStatusCode.NotFound, "Không tìm thấy"), _ => (HttpStatusCode.InternalServerError, "Lỗi hệ thống") };
ctx.Response = ctx.Request.CreateErrorResponse(status, msg);
}
}
// WebApiConfig.Register
config.Filters.Add(new ApiExceptionFilter());
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.