示例#1
0
        protected string urlByRoute(Applications.RequestContext context, string controllerActionOrRouteName, Dictionary <string, object> urlParamsDct)
        {
            Route  route  = this.urlRoutes[controllerActionOrRouteName];
            string result = context.Request.BasePath + route.Reverse.TrimEnd(new char[] { '?', '&' });
            Dictionary <string, object> allParams = new Dictionary <string, object>(urlParamsDct);

            foreach (var item in route.Params)
            {
                if (!allParams.ContainsKey(item.Key))
                {
                    allParams.Add(item.Key, item.Value);
                }
            }
            foreach (var item in allParams)
            {
                string paramKeyReplacement = "{%" + item.Key + "}";
                if (result.IndexOf(paramKeyReplacement) == -1)
                {
                    string glue = result.IndexOf('?') == -1 ? "?" : "&";
                    result += this.httpBuildQuery(new Dictionary <string, object>()
                    {
                        { item.Key, item.Value }
                    });
                }
                else
                {
                    result = result.Replace(paramKeyReplacement, item.Value.ToString());
                }
            }
            return(result);
        }
示例#2
0
 public virtual Response GetResponse()
 {
     Applications.RequestContext context = Application.GetRequestContext();
     if (context is Applications.RequestContext)
     {
         return(context.Response);
     }
     return(null);
 }
示例#3
0
 public virtual Controller GetController()
 {
     Applications.RequestContext context = Application.GetRequestContext();
     if (context is Applications.RequestContext)
     {
         return(context.Controller);
     }
     return(null);
 }
示例#4
0
 public virtual Route GetCurrentRoute()
 {
     Applications.RequestContext context = Application.GetRequestContext();
     if (context is Applications.RequestContext)
     {
         return(context.CurrentRoute);
     }
     return(null);
 }
示例#5
0
        protected string urlByQueryString(Applications.RequestContext context, string controllerActionOrRouteName, Dictionary <string, object> urlParamsDct)
        {
            string[] ctrlActionPascal    = controllerActionOrRouteName.Split(':');
            string   contollerPascalCase = ctrlActionPascal[0];
            string   actionPascalCase    = ctrlActionPascal[1];
            string   controllerDashed    = MvcCore.Tool.GetDashedFromPascalCase(contollerPascalCase);
            string   actionDashed        = MvcCore.Tool.GetDashedFromPascalCase(actionPascalCase);
            string   result = context.Request.BasePath + $"?controller={controllerDashed}&action={actionDashed}";

            result += this.httpBuildQuery(urlParamsDct);
            return(result);
        }
示例#6
0
 protected bool routeRequest()
 {
     Applications.RequestContext context = Application.GetRequestContext();
     if (!(context is Applications.RequestContext))
     {
         return(true);
     }
     try {
         context.CurrentRoute = Router.GetInstance().Route(context.Request);
         return(true);
     } catch (System.Exception e) {
         return(this.DispatchException(context, e));
     }
 }
示例#7
0
        public string Url(Applications.RequestContext context, string controllerActionOrRouteName = "Index:Index", object urlParams = null)
        {
            string result = "";
            Dictionary <string, object> requestParams = context.Request.Params;
            Dictionary <string, object> urlParamsDct  = new Dictionary <string, object>();

            if (controllerActionOrRouteName.IndexOf(":") > -1)
            {
                string[] ctrlActionPc = controllerActionOrRouteName.Split(':');
                string   ctrlPc       = ctrlActionPc[0];
                string   actionPc     = ctrlActionPc[1];
                if (ctrlPc.Length == 0)
                {
                    ctrlPc = MvcCore.Tool.GetPascalCaseFromDashed(requestParams["controller"].ToString());
                }
                if (actionPc.Length == 0)
                {
                    actionPc = MvcCore.Tool.GetPascalCaseFromDashed(requestParams["action"].ToString());
                }
                controllerActionOrRouteName = $"{ctrlPc}:{actionPc}";
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(urlParams);
                foreach (PropertyDescriptor prop in props)
                {
                    urlParamsDct.Add(prop.Name, prop.GetValue(urlParams));
                }
            }
            else if (controllerActionOrRouteName == "self")
            {
                controllerActionOrRouteName = context.CurrentRoute is Route ? context.CurrentRoute.Name : ":";
                PropertyDescriptorCollection props = TypeDescriptor.GetProperties(urlParams);
                foreach (PropertyDescriptor prop in props)
                {
                    urlParamsDct.Add(prop.Name, prop.GetValue(urlParams));
                }
                foreach (var item in requestParams)
                {
                    if (!urlParamsDct.ContainsKey(item.Key))
                    {
                        urlParamsDct.Add(item.Key, item.Value);
                    }
                }
                urlParamsDct.Remove("controller");
                urlParamsDct.Remove("action");
            }
            bool absolute = false;

            if (urlParamsDct.Count > 0 && urlParamsDct.ContainsKey("absolute"))
            {
                absolute = Convert.ToBoolean(urlParamsDct["absolute"]);
                urlParamsDct.Remove("absolute");
            }
            if (this.urlRoutes.ContainsKey(controllerActionOrRouteName))
            {
                result = this.urlByRoute(context, controllerActionOrRouteName, urlParamsDct);
            }
            else
            {
                result = this.urlByQueryString(context, controllerActionOrRouteName, urlParamsDct);
            }
            if (absolute)
            {
                result = context.Request.DomainUrl + result;
            }
            return(result);
        }