Пример #1
0
        public async Task OnRequested(IWebAPIContext context)
        {
            foreach (var middleware in this.middleware)
            {
                if (await middleware(context) == false)
                {
                    return;
                }
            }

            var paramInfos  = this.handler.GetParameters();
            var paramValues = paramInfos.Select(paramInfo => this.GetParameterValue(paramInfo, context)).ToArray();

            await(Task) this.handler.Invoke(instance, paramValues);
        }
Пример #2
0
        private object GetParameterValue(ParameterInfo parameterInfo, IWebAPIContext context)
        {
            switch (parameterInfo.Name)
            {
            case "context":
                return(context);

            case "request":
                return(context.Request);

            case "response":
                return(context.Response);

            case "body":
            {
                try
                {
                    if (typeof(JToken).IsAssignableFrom(parameterInfo.ParameterType))
                    {
                        return(context.ParseJson());
                    }

                    return(context.ParseBody(parameterInfo.ParameterType));
                }
                catch (Exception)
                {
                    throw new BadRequestException(string.Format("Expected body to be in the format of {0}.", parameterInfo.ParameterType.Name));
                }
            }
            }

            if (context.PathParameters.ContainsKey(parameterInfo.Name))
            {
                return(WebControllerRouter.ConvertPathParameter(parameterInfo.ParameterType, context.PathParameters[parameterInfo.Name]));
            }

            throw new Exception(string.Format("Unable to determine value for parameter '{0} of route method '{1}'", parameterInfo.Name, this.handler.Name));
        }