/// <summary> /// 添加Riven AspNet Core相关配置 /// </summary> /// <param name="services"></param> /// <param name="configurationAction"></param> /// <returns></returns> public static IServiceCollection AddRivenAspNetCore(this IServiceCollection services, Action <RivenAspNetCoreOptions> configurationAction = null) { var rivenAspNetCoreOptions = new RivenAspNetCoreOptions(); configurationAction?.Invoke(rivenAspNetCoreOptions); services.AddRivenAspNetCoreServices(rivenAspNetCoreOptions); return(services); }
static IServiceCollection AddRivenAspNetCoreServices(this IServiceCollection services, RivenAspNetCoreOptions rivenAspNetCoreOptions) { if (rivenAspNetCoreOptions.AuthorizationFilterEnable) { services.TryAddTransient <AppAuthorizationFilter>(); services.TryAddTransient <IAspNetCoreAuthorizationHandler, NullAspNetCoreAuthorizationHandler>(); services.Configure <MvcOptions>((options) => { options.Filters.AddService <AppAuthorizationFilter>(); }); } if (rivenAspNetCoreOptions.AuditFilterEnable) { services.TryAddTransient <AppAuditFilter>(); services.TryAddTransient <IAspNetCoreAuditHandler, NullAspNetCoreAuditHandler>(); services.Configure <MvcOptions>((options) => { options.Filters.AddService <AppAuditFilter>(); }); } if (rivenAspNetCoreOptions.ValidationFilterEnable) { services.TryAddTransient <AppValidationFilter>(); services.TryAddTransient <IAspNetCoreValidationHandler, NullAspNetCoreValidationHandler>(); services.Configure <MvcOptions>((options) => { options.Filters.AddService <AppValidationFilter>(); }); } if (rivenAspNetCoreOptions.UnitOfWorkFilterEnable) { services.TryAddTransient <AppUowFilter>(); services.TryAddTransient <IAspNetCoreUnitOfWorkHandler, NullAspNetCoreUnitOfWorkHandler>(); services.Configure <MvcOptions>((options) => { options.Filters.AddService <AppUowFilter>(); }); } if (rivenAspNetCoreOptions.ExceptionFilterEnable) { services.TryAddTransient <AppExceptionFilter>(); services.TryAddTransient <IAspNetCoreExceptionHandeler, NullAspNetCoreExceptionHandeler>(); services.Configure <MvcOptions>((options) => { options.Filters.AddService <AppExceptionFilter>(); }); } if (rivenAspNetCoreOptions.ResultFilterEnable) { services.TryAddTransient <AppResultFilter>(); services.TryAddTransient <IAspNetCoreResultHandler, NullAspNetCoreResultHandler>(); services.Configure <MvcOptions>((options) => { options.Filters.AddService <AppResultFilter>(); }); } return(services); }
protected virtual async Task HandleAndWrapException(HttpContext httpContext, Exception exception, RivenAspNetCoreOptions aspNetCoreOptions) { var jsonHelper = httpContext.RequestServices.GetRequiredService <IJsonHelper>(); httpContext.Response.Clear(); httpContext.Response.StatusCode = GetResponseStatusCode(httpContext, exception); httpContext.Response.OnStarting(ProcessCacheHeaders, httpContext.Response); // TODO: Use Consts httpContext.Response.Headers[HeaderNames.ContentType] = "application/json"; var errorInfo = CreateErrorInfo(httpContext, exception, aspNetCoreOptions); var htmlContent = jsonHelper.Serialize(new AjaxResponse <ErrorInfo>() { Code = httpContext.Response.StatusCode, Success = false, UnAuthorizedRequest = true, Result = errorInfo }); await httpContext.Response.WriteAsync(htmlContent.ToString(), httpContext.RequestAborted); }
/// <summary> /// 创建错误信息 /// </summary> /// <param name="httpContext"></param> /// <param name="exception"></param> /// <param name="aspNetCoreOptions"></param> /// <returns></returns> protected virtual ErrorInfo CreateErrorInfo(HttpContext httpContext, Exception exception, RivenAspNetCoreOptions aspNetCoreOptions) { if (exception is UserFriendlyException userFriendlyException) { return(new ErrorInfo(userFriendlyException.Message, userFriendlyException.Details)); } return(new ErrorInfo( exception.Message, aspNetCoreOptions.SendAllExceptionToClient ? exception.ToString() : nameof(HttpStatusCode.InternalServerError) )); }