コード例 #1
0
 public FetchHttpRequest(
     FetchHttpContext fetchContext,
     string path, string queryString, string method) :
     base(HttpManager.CurrentContext.Request)
 {
     FetchContext = fetchContext;
     FetchPath    = path;
     FetchMethod  = method;
     if (method == HttpMethods.GET)
     {
         FetchQueryString = queryString;
         FetchQuery       = HttpUtils.ParseQueryString(queryString);
         FetchForm        = new Dictionary <string, IList <string> >();
     }
     else
     {
         FetchQueryString = "";
         FetchQuery       = new Dictionary <string, IList <string> >();
         FetchForm        = HttpUtils.ParseQueryString(queryString);
     }
 }
コード例 #2
0
        /// <summary>
        /// Set template variable from action execute result
        /// </summary>
        public override void Render(Context context, TextWriter result)
        {
            // Replace variables in url
            var pathAndQuery = PathAndQuery;
            var matches      = VariableInUrlExpression.Matches(pathAndQuery);

            foreach (Match match in matches)
            {
                var    name  = match.Value.Substring(1, match.Value.Length - 2);
                string value = null;
                if (name == VariableNameForAllQueryParameters)
                {
                    value = HttpManager.CurrentContext.Request.QueryString;
                    if (value.Length > 0 && value[0] == '?')
                    {
                        value = value.Substring(1);
                    }
                }
                else if ((value = context[name].ConvertOrDefault <string>()) != null)
                {
                }
                else
                {
                    value = HttpManager.CurrentContext.Request.Get <string>(name);
                }
                pathAndQuery = pathAndQuery.Replace(match.Value, value);
            }
            // Find the target action from controllers
            var    controllerManager = Application.Ioc.Resolve <ControllerManager>();
            string path;
            string queryString;

            HttpUtils.SplitPathAndQuery(pathAndQuery, out path, out queryString);
            var method = HttpMethods.POST;
            var action = controllerManager.GetAction(path, method);

            if (action == null)
            {
                method = HttpMethods.GET;
                action = controllerManager.GetAction(path, method);
            }
            if (action == null)
            {
                throw new KeyNotFoundException($"action {path} not found");
            }
            // Execute action
            // Use simulate http context, inhert Items, Cookies, Headers
            var fetchContext = new FetchHttpContext(path, queryString, method);

            using (HttpManager.OverrideContext(fetchContext)) {
                var actionResult = action();
                if (actionResult is PlainResult)
                {
                    context[Variable] = ((PlainResult)actionResult).Text;
                }
                else if (actionResult is JsonResult)
                {
                    context[Variable] = ((JsonResult)actionResult).Object;
                }
                else
                {
                    var response = fetchContext.FetchResponse;
                    actionResult.WriteResponse(response);
                    context[Variable] = response.GetContentsFromBody();
                }
            }
        }