/// <summary> /// Called after an action has thrown an <see cref="T:System.Exception" />. /// </summary> /// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ExceptionContext" />.</param> /// <returns> /// A <see cref="T:System.Threading.Tasks.Task" /> that on completion indicates the filter has executed. /// </returns> public Task OnExceptionAsync(ExceptionContext context) { var response = new FailResultViewModel { Id = Guid.NewGuid().ToString(), Method = $"{context.HttpContext.Request.Path}.{context.HttpContext.Request.Method}", Status = "Error", Version = "1.0", Error = new FailInformation { Domain = "ProjectName", ErrorCode = 40000, Message = context.Exception.Message, Description = context.Exception.ToString() } }; context.Result = new ObjectResult(response) { // 500 StatusCode = (int)HttpStatusCode.InternalServerError }; // Exceptinon Filter只在ExceptionHandled=false時觸發 // 所以處理完Exception要標記true表示已處理 context.ExceptionHandled = true; return(Task.CompletedTask); }
/// <summary> /// Called early in the filter pipeline to confirm request is authorized. /// </summary> /// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext" />.</param> public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { var cookies = context.HttpContext.Request.Cookies; cookies.TryGetValue("token", out string token); if (token.Equals("123456")) { var response = new FailResultViewModel { Id = Guid.NewGuid().ToString(), Method = $"{context.HttpContext.Request.Path}.{context.HttpContext.Request.Method}", Status = "UnAuthorized", Version = "1.0", Error = new FailInformation() { Domain = "ProjectName", Message = "未授權", Description = "授權驗證失敗" } }; context.Result = new ObjectResult(response) { // 401 StatusCode = (int)HttpStatusCode.Unauthorized }; } }
/// <summary> /// Called asynchronously before the action, after model binding is complete. /// </summary> /// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext" />.</param> /// <param name="next">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate" />. Invoked to execute the next action filter or the action itself.</param> public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var parameter = context.ActionArguments.SingleOrDefault(); if (parameter.Value is null) { var response = new FailResultViewModel { Id = Guid.NewGuid().ToString(), Method = $"{context.HttpContext.Request.Path}.{context.HttpContext.Request.Method}", Status = "Error", Version = "1.0", Error = new FailInformation { Domain = "ProjectName", Message = "參數驗證失敗", Description = "傳入參數為null" } }; context.Result = new ObjectResult(response) { // 400 StatusCode = (int)HttpStatusCode.BadRequest }; } else { await next(); } }
/// <summary> /// On Exception /// </summary> /// <param name="context"></param> /// <returns></returns> public Task OnExceptionAsync(ExceptionContext context) { var failResultViewModel = new FailResultViewModel() { Id = Guid.NewGuid().ToString(), Method = $"{context.HttpContext.Request.Method} {context.HttpContext.Request.Path}", Version = "1.0", Status = "Error", Error = new FailInformation { ErrorCode = "4000", ErrorMessage = context.Exception.Message, Description = context.Exception.ToString() } }; context.Result = new ObjectResult(failResultViewModel); context.ExceptionHandled = true; return(Task.CompletedTask); }