/// <summary> /// 处理请求上下文 /// </summary> /// <returns></returns> private static async Task HandleRequestAsync(ApiRequestContext context) { // 参数验证 var validateProperty = context.HttpContext.HttpApiOptions.UseParameterPropertyValidate; foreach (var parameter in context.ApiAction.Parameters) { var parameterValue = context.Arguments[parameter.Index]; DataValidator.ValidateParameter(parameter, parameterValue, validateProperty); } // action特性请求前执行 foreach (var attr in context.ApiAction.Attributes) { await attr.OnRequestAsync(context).ConfigureAwait(false); } // 参数特性请求前执行 foreach (var parameter in context.ApiAction.Parameters) { var ctx = new ApiParameterContext(context, parameter); foreach (var attr in parameter.Attributes) { await attr.OnRequestAsync(ctx).ConfigureAwait(false); } } // Return特性请求前执行 foreach (var @return in context.ApiAction.Return.Attributes) { await @return.OnRequestAsync(context).ConfigureAwait(false); } // GlobalFilter请求前执行 foreach (var filter in context.HttpContext.HttpApiOptions.GlobalFilters) { await filter.OnRequestAsync(context).ConfigureAwait(false); } // Filter请求前执行 foreach (var filter in context.ApiAction.FilterAttributes) { await filter.OnRequestAsync(context).ConfigureAwait(false); } }
/// <summary> /// 创建请求委托 /// </summary> /// <param name="apiAction"></param> /// <returns></returns> private static InvokeDelegate <ApiRequestContext> BuildRequestHandler(ApiActionDescriptor apiAction) { var builder = new PipelineBuilder <ApiRequestContext>(); // 参数验证特性验证和参数模型属性特性验证 builder.Use(next => context => { var validateProperty = context.HttpContext.Options.UseParameterPropertyValidate; foreach (var parameter in context.ApiAction.Parameters) { var parameterValue = context.Arguments[parameter.Index]; DataValidator.ValidateParameter(parameter, parameterValue, validateProperty); } return(next(context)); }); // action特性请求前执行 foreach (var attr in apiAction.Attributes) { builder.Use(next => async context => { await attr.OnRequestAsync(context).ConfigureAwait(false); await next(context).ConfigureAwait(false); }); } // 参数特性请求前执行 foreach (var parameter in apiAction.Parameters) { var index = parameter.Index; foreach (var attr in parameter.Attributes) { builder.Use(next => async context => { var ctx = new ApiParameterContext(context, index); await attr.OnRequestAsync(ctx).ConfigureAwait(false); await next(context).ConfigureAwait(false); }); } } // Return特性请求前执行 foreach (var @return in apiAction.Return.Attributes) { builder.Use(next => async context => { await @return.OnRequestAsync(context).ConfigureAwait(false); await next(context).ConfigureAwait(false); }); } // Filter请求前执行 foreach (var filter in apiAction.FilterAttributes) { builder.Use(next => async context => { await filter.OnRequestAsync(context).ConfigureAwait(false); await next(context).ConfigureAwait(false); }); } return(builder.Build()); }