Base class for business controllers.
        internal static RequestParameters CreateParameters(BusinessControllerBase controller, HttpRequestMessage request)
        {
            controller.ThrowIfNull("controller");
            request.ThrowIfNull("request");

            Dictionary<string, object> postParameters = HttpMessageReader.ReadResponseAs<Dictionary<string, object>>(request);

            RequestParameters parameters = new RequestParameters();
            MethodInfo method = controller.GetControllerMethod(controller.Url.BusinessAction);
            ParameterInfo[] methodParams = method.GetParameters();
            for (int i = 0; i < methodParams.Length; i++)
            {
                object parameterValue;
                if (!controller.Url.Parameters.ContainsKey(methodParams[i].Name))
                {
                    if (postParameters.ContainsKey(methodParams[i].Name))
                    {
                        parameterValue = JsonConvert.DeserializeObject(postParameters[methodParams[i].Name].ToString(), methodParams[i].ParameterType);
                        //parameterValue = Convert.ChangeType(, );
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {

                    parameterValue = Convert.ChangeType(controller.Url.Parameters[methodParams[i].Name], methodParams[i].ParameterType);
                }
                parameters.AddParameter(methodParams[i].Name, parameterValue);
            }
            return parameters;
        }
Beispiel #2
0
        private Task<HttpResponseMessage> Invoke(BusinessControllerBase controller, RequestParameters parameter, HttpRequestMessage request)
        {
            controller.ThrowIfNull("controller");
            parameter.ThrowIfNull("parameter");

            return Task.Factory.StartNew<HttpResponseMessage>(() =>
            {
                //TODO: read content type from the given model's attribute.
                ResponseMessageContext context = ResponseMessageContext.GetContext(controller, parameter, request);
                try
                {
                    object result = controller.InvokeMethod(parameter);
                    context.Content = new JsonContent(result); // TODO: HttpContent's  should be get from the factory.
                    context.StatusCode = HttpStatusCode.OK;
                }
                catch (Exception ex)
                {
                    context.Content = new JsonContent(new { Error = ex.Message });
                    context.StatusCode = HttpStatusCode.InternalServerError;
                }
                HttpResponseMessage response = new HttpResponseMessage
                {
                    Content = context.Content,
                    RequestMessage = request,
                    StatusCode = context.StatusCode
                };
                return response;
            });
        }