/// <summary>
        /// Returns an object that provides methods to add routes to Kentico HTTP handlers.
        /// </summary>
        /// <param name="target">The instance of the <see cref="RouteCollection"/> class.</param>
        /// <returns>The object that provides methods to add routes to Kentico HTTP handlers.</returns>
        public static ExtensionPoint <RouteCollection> Kentico(this RouteCollection target)
        {
            lock (mLock)
            {
                if (mExtensionPoint == null || mExtensionPoint.Target != target)
                {
                    mExtensionPoint = new ExtensionPoint <RouteCollection>(target);
                }

                return(mExtensionPoint);
            }
        }
        /// <summary>
        /// Returns an object that provides methods to render HTML fragments.
        /// </summary>
        /// <param name="target">The instance of the <see cref="System.Web.Mvc.HtmlHelper"/> class.</param>
        /// <returns>The object that provides methods to render HTML fragments.</returns>
        public static ExtensionPoint <HtmlHelper> Kentico(this HtmlHelper target)
        {
            lock (mLock)
            {
                if (mExtensionPoint == null || mExtensionPoint.Target != target)
                {
                    mExtensionPoint = new ExtensionPoint <HtmlHelper>(target);
                }

                return(mExtensionPoint);
            }
        }
示例#3
0
        /// <summary>
        /// Generates a fully qualified URL to the specified attachment. If the attachment is an image, the URL points to a resized version.
        /// </summary>
        /// <param name="instance">The object that provides methods to build URLs to Kentico content.</param>
        /// <param name="attachment">The attachment.</param>
        /// <param name="constraint">The size constraint that is enforced on image when resizing.</param>
        /// <param name="options">Options that affect the attachment URL.</param>
        /// <returns>The fully qualified URL to the attachment.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="instance"/> or <paramref name="attachment"/> is null.</exception>
        public static string Attachment(this ExtensionPoint <UrlHelper> instance, Attachment attachment, SizeConstraint constraint, AttachmentUrlOptions options = null)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (attachment == null)
            {
                throw new ArgumentNullException("attachment");
            }

            return(GenerateAttachmentUrl(instance, attachment, constraint, options));
        }
示例#4
0
        /// <summary>
        /// Adds routes to Kentico HTTP handlers.
        /// </summary>
        /// <param name="instance">The object that provides methods to add routes to Kentico HTTP handlers.</param>
        /// <exception cref="ArgumentNullException"><paramref name="instance"/> is null.</exception>
        public static void MapRoutes(this ExtensionPoint <RouteCollection> instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var routes = instance.Target;

            using (routes.GetWriteLock())
            {
                foreach (var route in HttpHandlerRouteTable.Default.GetRoutes())
                {
                    routes.Add(route);
                }
            }
        }
示例#5
0
        /// <summary>
        /// Resolves relative URLs in the HTML fragment so that they can be used by the browser.
        /// </summary>
        /// <param name="instance">The object that provides methods to render HTML fragments.</param>
        /// <param name="html">An HTML fragment with potential relative URLs.</param>
        /// <returns>An HTML fragment where relative URLs are replaced with URLs that can be used by the browser.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="instance"/> is null.</exception>
        public static MvcHtmlString ResolveUrls(this ExtensionPoint <HtmlHelper> instance, string html)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            var urlHelper       = new UrlHelper(instance.Target.ViewContext.RequestContext);
            var applicationPath = urlHelper.Content("~/").TrimEnd('/');

            var pathIndex = html.IndexOfCSafe("~/");

            if (pathIndex >= 1)
            {
                var builder   = new StringBuilder((int)(html.Length * 1.1));
                var lastIndex = 0;

                while (pathIndex >= 1)
                {
                    if ((html[pathIndex - 1] == '(') || (html[pathIndex - 1] == '"') || (html[pathIndex - 1] == '\''))
                    {
                        // Add previous content
                        if (lastIndex < pathIndex)
                        {
                            builder.Append(html, lastIndex, pathIndex - lastIndex);
                        }

                        // Add application path and move to the next location
                        builder.Append(applicationPath);
                        lastIndex = pathIndex + 1;
                    }

                    pathIndex = html.IndexOfCSafe("~/", pathIndex + 2);
                }

                // Add the rest of the content
                if (lastIndex < html.Length)
                {
                    builder.Append(html, lastIndex, html.Length - lastIndex);
                }

                html = builder.ToString();
            }

            return(new MvcHtmlString(html));
        }
示例#6
0
        private static string GenerateAttachmentUrl(ExtensionPoint <UrlHelper> instance, Attachment attachment, SizeConstraint constraint, AttachmentUrlOptions options)
        {
            var fileName                   = GetFileName(attachment);
            var builder                    = new StringBuilder().AppendFormat("~/getattachment/{0:D}/{1}", attachment.GUID, GetFileNameForUrl(fileName));
            var referenceLength            = builder.Length;
            Action <string, object> append = (name, value) =>
            {
                builder.Append(builder.Length == referenceLength ? '?' : '&').Append(name).Append('=').Append(value);
            };

            if (constraint.WidthComponent > 0)
            {
                append("width", constraint.WidthComponent);
            }

            if (constraint.HeightComponent > 0)
            {
                append("height", constraint.HeightComponent);
            }

            if (constraint.MaxWidthOrHeightComponent > 0)
            {
                append("maxsidesize", constraint.MaxWidthOrHeightComponent);
            }

            // Prevent Kentico from using site settings
            if (!constraint.IsEmpty)
            {
                append("resizemode", "force");
            }

            if ((options != null) && options.AttachmentContentDisposition)
            {
                append("disposition", "attachment");
            }

            var url = builder.ToString();

            if (attachment.VersionID > 0)
            {
                url = GetAttachmentPreviewUrl(url);
            }

            return(instance.Target.Content(url));
        }