public async Task InvokeAsync(HttpContext httpContext)
        {
            var routingResult = ControllerRouter.Route(httpContext.Request.Path.ToString());

            if (routingResult == null)
            {
                await Next(httpContext);

                return;
            }

            var instance = Instantiator.Instantiate(routingResult.Controller.Type);

            IMethodParameterProvider controllerActionParameterProvider = new MethodParameterProvider(new MethodParameterValueProvider(httpContext.Request.Query.Keys.ToDictionary(k => k, k => httpContext.Request.Query[k].First()), () => new StreamReader(httpContext.Request.Body, Encoding.UTF8).ReadToEnd()));

            var parameters = controllerActionParameterProvider.GetParameters(routingResult.Action.Method);

            var result = routingResult.Action.Method.Invoke(instance, parameters);

            if (result != null)
            {
                httpContext.Response.ContentType = "application/json";

                await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(result));
            }
        }
Exemplo n.º 2
0
        public ActionResult Index()
        {
            var routingResult = ControllerContext.RouteData.DataTokens["PoetryRoutingResult"] as ControllerRouterResult;

            if (routingResult == null)
            {
                return(HttpNotFound("Request was not routed through Poetry - something seems to be wrong in your routing."));
            }

            var instance = DependencyResolver.Current.GetService(routingResult.Controller.Type);

            IMethodParameterProvider controllerActionParameterProvider = new MethodParameterProvider(new MethodParameterValueProvider(Request.QueryString.AllKeys.ToDictionary(t => t, t => Request[t]), () => new StreamReader(Request.InputStream, Encoding.UTF8).ReadToEnd()));

            var parameters = controllerActionParameterProvider.GetParameters(routingResult.Action.Method);

            var result = routingResult.Action.Method.Invoke(instance, parameters);

            if (result != null)
            {
                return(Content(JsonConvert.SerializeObject(result), "application/json"));
            }
            else
            {
                return(new EmptyResult());
            }
        }