Пример #1
0
        public Uri CreateUriForRoute(SiteRoute sr, UriSettings settings, IDictionary <string, string> parameters)
        {
            var requireSsl = false;

            if (sr is ContentPageRoute cpr)
            {
                requireSsl = cpr.RequireSsl;
            }

            var needsAuthorityChange = (sr.Authority != UriAuthorityFilter.Any && _context.Authority != sr.Authority);
            var needsSslChange       = !_context.IsSsl && requireSsl;

            if (settings.PreferUriKind != UriKinds.AbsoluteUri && !needsSslChange && !needsAuthorityChange)
            {
                if (settings.PreferUriKind == UriKinds.RelativeFromSiteRoot)
                {
                    return(sr.VirtualPath);
                }

                if (settings.PreferUriKind == UriKinds.RelativeFromCurrent)
                {
                    return(_context.AbsolutePath.MakeRelativeUri(sr.VirtualPath));
                }
            }


            var protocol = "http://";

            if (requireSsl)
            {
                protocol = "https://";
            }

            var authority = sr.Authority;

            if (sr.Authority == UriAuthorityFilter.Any)
            {
                authority = _context.Authority;
            }

            var uriBase = new Uri($"{protocol}{authority}{sr.VirtualPath}");
            var builder = new UriBuilder(uriBase);

            if (parameters != null && parameters.Count > 0)
            {
                builder.Query = new FormUrlEncodedContent(parameters).ReadAsStringAsync().Result;
            }

            return(builder.Uri);
        }
Пример #2
0
        public Uri CreateUri(CmsPage destinationPage, UriSettings settings, IDictionary <string, string> parameters)
        {
            if (destinationPage == null)
            {
                throw new ArgumentNullException(nameof(destinationPage));
            }

            SiteRoute sr;
            var       success = CmsRoutes.Current.TryResolveRoute(destinationPage.ContentId, out sr);

            if (!success)
            {
                throw new Exception($"Could not resolve a content route for {destinationPage.Name}, id: {destinationPage.ContentId}");
            }

            return(CreateUriForRoute(sr, settings, parameters));
        }