Example #1
0
        public static ActionResult CreateRedirectResult(Site site, FrontRequestChannel channel, string url, string rawUrl, int? statusCode, RedirectType redirectType)
        {
            var redirectUrl = url;
            if (!UrlUtility.IsAbsoluteUrl(redirectUrl))
            {
                redirectUrl = UrlUtility.ResolveUrl(redirectUrl);
                //WrapperUrl will cause endless loop if site host by ASP.NET development server when transfer redirect.
                if (redirectType != RedirectType.Transfer || Settings.IsHostByIIS)
                {
                    redirectUrl = FrontUrlHelper.WrapperUrl(redirectUrl, site, channel).ToString();
                }
            }
            if (!string.IsNullOrEmpty(rawUrl))
            {
                redirectUrl = redirectUrl.AddQueryParam("errorpath", rawUrl);
            }
            if (statusCode != null)
            {
                redirectUrl = redirectUrl.AddQueryParam("statusCode", statusCode.ToString());
            }

            switch (redirectType)
            {
                case RedirectType.Moved_Permanently_301:
                    return new Redirect301Result(redirectUrl);

                case RedirectType.Transfer:
                    return new TransferResult(redirectUrl, statusCode ?? 200);
                case RedirectType.Found_Redirect_302:
                default:
                    return new RedirectResult(redirectUrl);
            }
        }
Example #2
0
        public static IHtmlString GeneratePageUrl(UrlHelper urlHelper, Site site, Page page, object values, FrontRequestChannel channel)
        {
            RouteValueDictionary routeValues = RouteValuesHelpers.GetRouteValues(values);

            page = page.AsActual();

            if (page == null)
            {
                return new HtmlString("");
            }
            if (page.Route != null && !string.IsNullOrEmpty(page.Route.ExternalUrl))
            {
                return new HtmlString(page.Route.ExternalUrl);
            }

            var pageRoute = page.Route.ToMvcRoute();

            routeValues = RouteValuesHelpers.MergeRouteValues(pageRoute.Defaults, routeValues);

            var routeVirtualPath = pageRoute.GetVirtualPath(urlHelper.RequestContext, routeValues);
            if (routeVirtualPath == null)
            {
                throw new InvalidPageRouteException(page);
            }
            //string contentUrl = routeVirtualPath.VirtualPath;//don't decode the url. why??
            //if do not decode the url, the route values contains Chinese character will cause bad request.
            string contentUrl = HttpUtility.UrlDecode(routeVirtualPath.VirtualPath);
            string pageUrl = contentUrl;
            if (!string.IsNullOrEmpty(contentUrl) || (string.IsNullOrEmpty(pageUrl) && !page.IsDefault))
            {
                pageUrl = Kooboo.Web.Url.UrlUtility.Combine(page.VirtualPath, contentUrl);
            }
            if (string.IsNullOrEmpty(pageUrl))
            {
                pageUrl = urlHelper.Content("~/");
            }
            else
            {
                pageUrl = HttpUtility.UrlDecode(
                urlHelper.RouteUrl("Page", new { PageUrl = new HtmlString(pageUrl) }));
            }
            var url = FrontUrlHelper.WrapperUrl(pageUrl, site, channel, page.RequireHttps);

            return url;
        }
Example #3
0
 public FrontUrlHelper(UrlHelper url, Site site, FrontRequestChannel requestChannel)
 {
     this.Url = url;
     this.Site = site;
     this.RequestChannel = requestChannel;
 }
Example #4
0
        public static IHtmlString WrapperUrl(string url, Site site, FrontRequestChannel channel, bool? requireSSL)
        {
            if (string.IsNullOrEmpty(url))
            {
                return new HtmlString(url);
            }
            var applicationPath = HttpContext.Current.Request.ApplicationPath.TrimStart(new char[] { '/' });
            if (!url.StartsWith("/") && !string.IsNullOrEmpty(applicationPath))
            {
                url = "/" + applicationPath + "/" + url;
            }

            var sitePath = site.AsActual().SitePath;
            if (channel == FrontRequestChannel.Debug || channel == FrontRequestChannel.Design || channel == FrontRequestChannel.Unknown)
            {
                sitePath = SiteHelper.PREFIX_FRONT_DEBUG_URL + site.FullName;
            }
            var urlSplit = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            IEnumerable<string> urlPaths = urlSplit;
            if (!string.IsNullOrEmpty(sitePath))
            {
                if (urlSplit.Length > 0 && applicationPath.EqualsOrNullEmpty(urlSplit[0], StringComparison.OrdinalIgnoreCase))
                {
                    urlPaths = new string[] { applicationPath, sitePath }.Concat(urlSplit.Skip(1));
                }
                else
                {
                    urlPaths = new string[] { sitePath }.Concat(urlSplit);
                }
            }
            var endWithSlash = url.EndsWith("/");
            url = "/" + string.Join("/", urlPaths.ToArray());
            if (endWithSlash && !url.EndsWith("/"))
            {
                url = url + "/";
            }

            #region SSL
            if (requireSSL.HasValue)
            {
                if (channel == FrontRequestChannel.Host || channel == FrontRequestChannel.HostNPath)
                {
                    if (HttpContext.Current.Request.IsSecureConnection)
                    {
                        //if (!requireSSL.Value)
                        //{
                        //    url = "http://" + HttpContext.Current.Request.Url.Host + url;
                        //}
                    }
                    else if (requireSSL.Value)
                    {
                        url = "https://" + HttpContext.Current.Request.Url.Host + url;
                    }
                }
            }

            #endregion

            return new HtmlString(url);
        }
Example #5
0
 public static IHtmlString WrapperUrl(string url, Site site, FrontRequestChannel channel)
 {
     return WrapperUrl(url, site, channel, false);
 }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PageRequestContext" /> class.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="rawSite">The raw.</param>
        /// <param name="site">The site.</param>
        /// <param name="page">The page.</param>
        /// <param name="requestChannel">The request channel.</param>
        /// <param name="pageRequestUrl">The page request url with out page virtual path.</param>
        public PageRequestContext(ControllerContext controllerContext, Site rawSite, Site site, Page rawPage, Page page, FrontRequestChannel requestChannel, string pageRequestUrl)
        {
            RouteValues = new RouteValueDictionary();

            this.ControllerContext = controllerContext;

            var httpContext = HttpContext.Current;

            this.RawSite = rawSite;
            this.RawPage = rawPage;
            this.Site = site.AsActual();
            this.Page = page.AsActual();
            this.RequestChannel = requestChannel;

            this.AllQueryString = new NameValueCollection();
            var queryString = httpContext.Request.QueryString;
            foreach (var key in queryString.AllKeys)
            {
                var value = queryString[key];
                if (!string.IsNullOrEmpty(value))
                {
                    AllQueryString[HttpUtility.HtmlEncode(key)] = HttpUtility.HtmlEncode(value);
                }
                else
                {
                    AllQueryString[HttpUtility.HtmlEncode(key)] = value;
                }

            }

            HttpContextBase pageContext = new PageHttpContenxt(httpContext, new PageHttpRequest(httpContext.Request, "~/" + pageRequestUrl, ""));
            var routeData = page.Route.ToMvcRoute().GetRouteData(pageContext);

            if (routeData != null)
            {
                RouteValues = routeData.Values;
                //Combine page parameters to [QueryString].
                foreach (var item in RouteValues)
                {
                    if (item.Value != null)
                    {
                        AllQueryString[item.Key] = item.Value.ToString();
                    }
                }
            }

            var moduleUrl = AllQueryString[ModuleUrlContext.ModuleUrlSegment];
            ModuleUrlContext = new ModuleUrlContext(this, moduleUrl);
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PageRequestContext" /> class.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="site">The site.</param>
 /// <param name="page">The page.</param>
 /// <param name="requestChannel">The request channel.</param>
 /// <param name="pageRequestUrl">The page request URL.</param>
 public PageRequestContext(ControllerContext controllerContext, Site site, Page page, FrontRequestChannel requestChannel, string pageRequestUrl)
     : this(controllerContext, site, site, page, page, requestChannel, pageRequestUrl)
 {
 }
Example #8
0
 public static FrontUrlHelper FrontUrl(this UrlHelper url, Site site, FrontRequestChannel requestChannel = FrontRequestChannel.Host)
 {
     return new FrontUrlHelper(url, site, requestChannel);
 }
Example #9
0
 public static FrontUrlHelper FrontUrl(this UrlHelper url, Site site, FrontRequestChannel requestChannel = FrontRequestChannel.Host)
 {
     return(new FrontUrlHelper(url, site, requestChannel));
 }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PageRequestContext" /> class.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="site">The site.</param>
 /// <param name="page">The page.</param>
 /// <param name="requestChannel">The request channel.</param>
 /// <param name="pageRequestUrl">The page request URL.</param>
 public PageRequestContext(ControllerContext controllerContext, Site site, Page page, FrontRequestChannel requestChannel, string pageRequestUrl)
     : this(controllerContext, site, site, page, page, requestChannel, pageRequestUrl)
 {
 }
Example #11
0
        public static IHtmlString WrapperUrl(string url, Site site, FrontRequestChannel channel, bool?requireSSL)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(new HtmlString(url));
            }
            var applicationPath = HttpContext.Current.Request.ApplicationPath.TrimStart(new char[] { '/' });

            if (!url.StartsWith("/") && !string.IsNullOrEmpty(applicationPath))
            {
                url = "/" + applicationPath + "/" + url;
            }


            var sitePath = site.AsActual().SitePath;

            if (channel == FrontRequestChannel.Debug || channel == FrontRequestChannel.Design || channel == FrontRequestChannel.Unknown)
            {
                sitePath = SiteHelper.PREFIX_FRONT_DEBUG_URL + site.FullName;
            }
            var urlSplit = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            IEnumerable <string> urlPaths = urlSplit;

            if (!string.IsNullOrEmpty(sitePath))
            {
                if (urlSplit.Length > 0 && applicationPath.EqualsOrNullEmpty(urlSplit[0], StringComparison.OrdinalIgnoreCase))
                {
                    urlPaths = new string[] { applicationPath, sitePath }.Concat(urlSplit.Skip(1));
                }
                else
                {
                    urlPaths = new string[] { sitePath }.Concat(urlSplit);
                }
            }
            var endWithSlash = url.EndsWith("/");

            url = "/" + string.Join("/", urlPaths.ToArray());
            if (endWithSlash && !url.EndsWith("/"))
            {
                url = url + "/";
            }


            #region SSL
            if (requireSSL.HasValue)
            {
                if (channel == FrontRequestChannel.Host || channel == FrontRequestChannel.HostNPath)
                {
                    if (HttpContext.Current.Request.IsSecureConnection)
                    {
                        //if (!requireSSL.Value)
                        //{
                        //    url = "http://" + HttpContext.Current.Request.Url.Host + url;
                        //}
                    }
                    else if (requireSSL.Value)
                    {
                        url = "https://" + HttpContext.Current.Request.Url.Host + url;
                    }
                }
            }


            #endregion

            return(new HtmlString(url));
        }
Example #12
0
 public static IHtmlString WrapperUrl(string url, Site site, FrontRequestChannel channel)
 {
     return(WrapperUrl(url, site, channel, false));
 }
Example #13
0
        public static IHtmlString GeneratePageUrl(UrlHelper urlHelper, Site site, Page page, object values, FrontRequestChannel channel)
        {
            RouteValueDictionary routeValues = RouteValuesHelpers.GetRouteValues(values);

            page = page.AsActual();

            if (page == null)
            {
                return(new HtmlString(""));
            }
            if (page.Route != null && !string.IsNullOrEmpty(page.Route.ExternalUrl))
            {
                return(new HtmlString(page.Route.ExternalUrl));
            }

            var pageRoute = page.Route.ToMvcRoute();

            routeValues = RouteValuesHelpers.MergeRouteValues(pageRoute.Defaults, routeValues);

            var routeVirtualPath = pageRoute.GetVirtualPath(urlHelper.RequestContext, routeValues);

            if (routeVirtualPath == null)
            {
                throw new InvalidPageRouteException(page);
            }
            //string contentUrl = routeVirtualPath.VirtualPath;//don't decode the url. why??
            //if do not decode the url, the route values contains Chinese character will cause bad request.
            string contentUrl = HttpUtility.UrlDecode(routeVirtualPath.VirtualPath);
            string pageUrl    = contentUrl;

            if (!string.IsNullOrEmpty(contentUrl) || (string.IsNullOrEmpty(pageUrl) && !page.IsDefault))
            {
                pageUrl = Kooboo.Web.Url.UrlUtility.Combine(page.VirtualPath, contentUrl);
            }
            if (string.IsNullOrEmpty(pageUrl))
            {
                pageUrl = urlHelper.Content("~/");
            }
            else
            {
                pageUrl = HttpUtility.UrlDecode(
                    urlHelper.RouteUrl("Page", new { PageUrl = new HtmlString(pageUrl) }));
            }
            var url = FrontUrlHelper.WrapperUrl(pageUrl, site, channel, page.RequireHttps);

            return(url);
        }
Example #14
0
 public FrontUrlHelper(UrlHelper url, Site site, FrontRequestChannel requestChannel)
 {
     this.Url            = url;
     this.Site           = site;
     this.RequestChannel = requestChannel;
 }
Example #15
0
        internal IHtmlString GeneratePageUrl(UrlHelper urlHelper, Site site, Page page, object values, FrontRequestChannel channel)
        {
            RouteValueDictionary routeValues = RouteValuesHelper.GetRouteValues(values);

            if (page == null)
            {
                return(new HtmlString(""));
            }
            var route = page.Routes == null ? null : page.Routes.FirstOrDefault();
            //if (route != null && !string.IsNullOrEmpty(route.ExternalUrl))
            //{
            //    return new HtmlString(route.ExternalUrl);
            //}

            var pageRoute = route.ToMvcRoute();

            routeValues = RouteValuesHelper.MergeRouteValues(pageRoute.Defaults, routeValues);

            var routeVirtualPath = pageRoute.GetVirtualPath(urlHelper.RequestContext, routeValues);

            if (routeVirtualPath == null)
            {
                Kooboo.Common.Logging.Logger.LoggerInstance.Warn(string.Format("Invalid page URL route. Page:{0}", page.AbsoluteName));
            }
            //string contentUrl = routeVirtualPath.VirtualPath;//don't decode the url. why??
            //if do not decode the url, the route values contains Chinese character will cause bad request.
            string contentUrl = HttpUtility.UrlDecode(routeVirtualPath.VirtualPath);
            string pageUrl    = contentUrl;

            if (!string.IsNullOrEmpty(contentUrl) || (string.IsNullOrEmpty(pageUrl) && !page.IsDefault))
            {
                pageUrl = Kooboo.Common.Web.UrlUtility.Combine(page.GetVirtualPath(route), contentUrl);
            }
            if (string.IsNullOrEmpty(pageUrl))
            {
                pageUrl = urlHelper.Content("~/");
            }
            else
            {
                pageUrl = HttpUtility.UrlDecode(
                    urlHelper.RouteUrl("Page", new { PageUrl = new HtmlString(pageUrl) }));
            }
            var url = this.WrapperUrl(pageUrl, page.RequireHttps);

            return(url);
        }
Example #16
0
        public static IHtmlString WrapperUrl(string url, Site site, FrontRequestChannel channel)
        {
            if (string.IsNullOrEmpty(url))
            {
                return new HtmlString(url);
            }
            var applicationPath = HttpContext.Current.Request.ApplicationPath.TrimStart(new char[] { '/' });
            if (!url.StartsWith("/") && !string.IsNullOrEmpty(applicationPath))
            {
                url = "/" + applicationPath + "/" + url;
            }
            var urlSplit = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            var sitePath = site.AsActual().SitePath;
            if(channel == FrontRequestChannel.Debug || channel == FrontRequestChannel.Design || channel == FrontRequestChannel.Unknown)
            {
                sitePath = SiteHelper.PREFIX_FRONT_DEBUG_URL + site.FullName;
            }
            IEnumerable<string> urlPaths = urlSplit;
            if (!string.IsNullOrEmpty(sitePath))
            {
                if (urlSplit.Length > 0 && applicationPath.EqualsOrNullEmpty(urlSplit[0], StringComparison.OrdinalIgnoreCase))
                {
                    urlPaths = new string[] { applicationPath, sitePath }.Concat(urlSplit.Skip(1));
                }
                else
                {
                    urlPaths = new string[] { sitePath }.Concat(urlSplit);
                }
            }

            url = "/" + string.Join("/", urlPaths.ToArray());
            return new HtmlString(url);
        }
Example #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FrontRequestContext"/> class.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <param name="page">The page.</param>
        /// <param name="requestChannel">The request channel.</param>
        /// <param name="httpContext">The HTTP context.</param>
        /// <param name="pageRequestUrl">The page request url with out page virtual path.</param>
        public PageRequestContext(ControllerContext controllerContext, Site site, Page page, FrontRequestChannel requestChannel, string pageRequestUrl)
        {
            RouteValues = new RouteValueDictionary();

            this.ControllerContext = controllerContext;

            var httpContext = HttpContext.Current;

            this.Site = site.AsActual();
            this.Page = page.AsActual();
            this.RequestChannel = requestChannel;

            this.AllQueryString = new NameValueCollection(httpContext.Request.QueryString);

            HttpContextBase pageContext = new PageHttpContenxt(httpContext, new PageHttpRequest(httpContext.Request, "~/" + pageRequestUrl, ""));
            var routeData = page.Route.ToMvcRoute().GetRouteData(pageContext);

            if (routeData != null)
            {
                RouteValues = routeData.Values;
                //Combine page parameters to [QueryString].
                foreach (var item in RouteValues)
                {
                    if (item.Value != null)
                    {
                        AllQueryString[item.Key] = item.Value.ToString();
                    }
                }
            }

            var moduleUrl = AllQueryString[ModuleUrlSegment];
            ModuleUrlContext = new ModuleUrlContext(this, moduleUrl);
        }
Example #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FrontRequestContext"/> class.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <param name="page">The page.</param>
        /// <param name="requestChannel">The request channel.</param>
        /// <param name="httpContext">The HTTP context.</param>
        /// <param name="pageRequestUrl">The page request url with out page virtual path.</param>
        public PageRequestContext(ControllerContext controllerContext, Site site, Page page, FrontRequestChannel requestChannel, string pageRequestUrl)
        {
            RouteValues = new RouteValueDictionary();

            this.ControllerContext = controllerContext;

            var httpContext = HttpContext.Current;

            this.Site           = site.AsActual();
            this.Page           = page.AsActual();
            this.RequestChannel = requestChannel;

            this.AllQueryString = new NameValueCollection(httpContext.Request.QueryString);


            HttpContextBase pageContext = new PageHttpContenxt(httpContext, new PageHttpRequest(httpContext.Request, "~/" + pageRequestUrl, ""));
            var             routeData   = page.Route.ToMvcRoute().GetRouteData(pageContext);

            if (routeData != null)
            {
                RouteValues = routeData.Values;
                //Combine page parameters to [QueryString].
                foreach (var item in RouteValues)
                {
                    if (item.Value != null)
                    {
                        AllQueryString[item.Key] = item.Value.ToString();
                    }
                }
            }

            var moduleUrl = AllQueryString[ModuleUrlSegment];

            ModuleUrlContext = new ModuleUrlContext(this, moduleUrl);
        }