/// <summary> /// 自定義錯誤狀態嗎 /// </summary> public class CustomeException : UserFriendlyException { public CustomeException(int code, string message, string details) : this((int)HttpStatusCode.BadRequest, code, message, details) { } public CustomeException(HttpStatusCode httpCode, int code, string message, string details) : this((int)httpCode, code, message, details) { } public CustomeException(int httpCode, int code, string message, string details) : base(code, message, details) { HttpCode = httpCode; } /// <summary> /// http 狀態碼 /// </summary> public int HttpCode { get; set; } }
public class CustomeApiExceptionFilterAttribute : AbpApiExceptionFilterAttribute, ITransientDependency { public CustomeApiExceptionFilterAttribute(IAbpWebApiConfiguration configuration) : base(configuration) { } protected override HttpStatusCode GetStatusCode(HttpActionExecutedContext context) { var customException = (context.Exception as CustomeException); if (customException != null) { return (HttpStatusCode)customException.HttpCode; } else { return base.GetStatusCode(context); } } public void UseThis() { var filters = Configuration.HttpConfiguration.Filters; var filterInfo = filters.FirstOrDefault(h => h.Instance is AbpApiExceptionFilterAttribute); if (filterInfo != null) { filters.Remove(filterInfo.Instance); } filters.Add(this); } }