/// <summary>
        /// Tries to resolve the given <paramref name="url"/> value relative to the application's 'webroot' setting.
        /// </summary>
        /// <param name="url">The URL to resolve.</param>
        /// <param name="resolvedUrl">
        /// Absolute URL beginning with the application's virtual root. <c>null</c> if <paramref name="url"/> could
        /// not be resolved.
        /// </param>
        /// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns>
        protected bool TryResolveUrl(string url, out IHtmlContent resolvedUrl)
        {
            resolvedUrl = null;
            var start = FindRelativeStart(url);

            if (start == -1)
            {
                return(false);
            }

            url = url.Remove(start, 2).Insert(start, $"~/themes/{Theme.Key}/");
            var trimmedUrl = CreateTrimmedString(url, start);



            var urlHelper              = UrlHelperFactory.GetUrlHelper(ViewContext);
            var appRelativeUrl         = urlHelper.Content(trimmedUrl);
            var postTildeSlashUrlValue = trimmedUrl.Substring(2);

            if (!appRelativeUrl.EndsWith(postTildeSlashUrlValue, StringComparison.Ordinal))
            {
                throw new InvalidOperationException("");
            }

            resolvedUrl = new EncodeFirstSegmentContent(
                appRelativeUrl,
                appRelativeUrl.Length - postTildeSlashUrlValue.Length,
                postTildeSlashUrlValue);

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Tries to resolve the given <paramref name="url"/> value relative to the application's 'webroot' setting.
        /// </summary>
        /// <param name="url">The URL to resolve.</param>
        /// <param name="resolvedUrl">
        /// Absolute URL beginning with the application's virtual root. <c>null</c> if <paramref name="url"/> could
        /// not be resolved.
        /// </param>
        /// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns>
        protected bool TryResolveUrl(string url, out IHtmlContent resolvedUrl)
        {
            resolvedUrl = null;
            var start = FindRelativeStart(url);

            if (start == -1)
            {
                return(false);
            }

            var trimmedUrl = CreateTrimmedString(url, start);

            var urlHelper              = UrlHelperFactory.GetUrlHelper(ViewContext);
            var appRelativeUrl         = urlHelper.Content(trimmedUrl);
            var postTildeSlashUrlValue = trimmedUrl.Substring(2);

            if (!appRelativeUrl.EndsWith(postTildeSlashUrlValue, StringComparison.Ordinal))
            {
                throw new InvalidOperationException(
                          Resources.FormatCouldNotResolveApplicationRelativeUrl_TagHelper(
                              url,
                              nameof(IUrlHelper),
                              nameof(IUrlHelper.Content),
                              "removeTagHelper",
                              typeof(UrlResolutionTagHelper).FullName,
                              typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly.GetName().Name));
            }

            resolvedUrl = new EncodeFirstSegmentContent(
                appRelativeUrl,
                appRelativeUrl.Length - postTildeSlashUrlValue.Length,
                postTildeSlashUrlValue);

            return(true);
        }
        /// <summary>
        /// Tries to resolve the given <paramref name="url"/> value relative to the application's 'webroot' setting.
        /// </summary>
        /// <param name="url">The URL to resolve.</param>
        /// <param name="resolvedUrl">
        /// Absolute URL beginning with the application's virtual root. <c>null</c> if <paramref name="url"/> could
        /// not be resolved.
        /// </param>
        /// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns>
        protected bool TryResolveUrl(string url, out IHtmlContent resolvedUrl)
        {
            resolvedUrl = null;
            if (url == null)
            {
                return(false);
            }

            var trimmedUrl = url.Trim(ValidAttributeWhitespaceChars);

            // Before doing more work, ensure that the URL we're looking at is app-relative.
            if (trimmedUrl.Length >= 2 && trimmedUrl[0] == '~' && trimmedUrl[1] == '/')
            {
                var urlHelper              = UrlHelperFactory.GetUrlHelper(ViewContext);
                var appRelativeUrl         = urlHelper.Content(trimmedUrl);
                var postTildeSlashUrlValue = trimmedUrl.Substring(2);

                if (!appRelativeUrl.EndsWith(postTildeSlashUrlValue, StringComparison.Ordinal))
                {
                    throw new InvalidOperationException(
                              Resources.FormatCouldNotResolveApplicationRelativeUrl_TagHelper(
                                  url,
                                  nameof(IUrlHelper),
                                  nameof(IUrlHelper.Content),
                                  "removeTagHelper",
                                  typeof(UrlResolutionTagHelper).FullName,
                                  typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly.GetName().Name));
                }

                resolvedUrl = new EncodeFirstSegmentContent(
                    appRelativeUrl,
                    appRelativeUrl.Length - postTildeSlashUrlValue.Length,
                    postTildeSlashUrlValue);

                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Tries to resolve the given <paramref name="url"/> value relative to the application's 'webroot' setting.
        /// </summary>
        /// <param name="url">The URL to resolve.</param>
        /// <param name="resolvedUrl">
        /// Absolute URL beginning with the application's virtual root. <c>null</c> if <paramref name="url"/> could
        /// not be resolved.
        /// </param>
        /// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns>
        protected bool TryResolveUrl(string url, out IHtmlContent resolvedUrl)
        {
            resolvedUrl = null;
            var start = FindRelativeStart(url);
            if (start == -1)
            {
                return false;
            }

            var trimmedUrl = CreateTrimmedString(url, start);

            var urlHelper = UrlHelperFactory.GetUrlHelper(ViewContext);
            var appRelativeUrl = urlHelper.Content(trimmedUrl);
            var postTildeSlashUrlValue = trimmedUrl.Substring(2);

            if (!appRelativeUrl.EndsWith(postTildeSlashUrlValue, StringComparison.Ordinal))
            {
                throw new InvalidOperationException(
                    Resources.FormatCouldNotResolveApplicationRelativeUrl_TagHelper(
                        url,
                        nameof(IUrlHelper),
                        nameof(IUrlHelper.Content),
                        "removeTagHelper",
                        typeof(UrlResolutionTagHelper).FullName,
                        typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly.GetName().Name));
            }

            resolvedUrl = new EncodeFirstSegmentContent(
                appRelativeUrl,
                appRelativeUrl.Length - postTildeSlashUrlValue.Length,
                postTildeSlashUrlValue);

            return true;
        }
        /// <summary>
        /// Tries to resolve the given <paramref name="url"/> value relative to the application's 'webroot' setting.
        /// </summary>
        /// <param name="url">The URL to resolve.</param>
        /// <param name="resolvedUrl">
        /// Absolute URL beginning with the application's virtual root. <c>null</c> if <paramref name="url"/> could
        /// not be resolved.
        /// </param>
        /// <returns><c>true</c> if the <paramref name="url"/> could be resolved; <c>false</c> otherwise.</returns>
        protected bool TryResolveUrl(string url, out IHtmlContent resolvedUrl)
        {
            resolvedUrl = null;
            if (url == null)
            {
                return false;
            }

            var trimmedUrl = url.Trim(ValidAttributeWhitespaceChars);

            // Before doing more work, ensure that the URL we're looking at is app-relative.
            if (trimmedUrl.Length >= 2 && trimmedUrl[0] == '~' && trimmedUrl[1] == '/')
            {
                var urlHelper = UrlHelperFactory.GetUrlHelper(ViewContext);
                var appRelativeUrl = urlHelper.Content(trimmedUrl);
                var postTildeSlashUrlValue = trimmedUrl.Substring(2);

                if (!appRelativeUrl.EndsWith(postTildeSlashUrlValue, StringComparison.Ordinal))
                {
                    throw new InvalidOperationException(
                        Resources.FormatCouldNotResolveApplicationRelativeUrl_TagHelper(
                            url,
                            nameof(IUrlHelper),
                            nameof(IUrlHelper.Content),
                            "removeTagHelper",
                            typeof(UrlResolutionTagHelper).FullName,
                            typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly.GetName().Name));
                }

                resolvedUrl = new EncodeFirstSegmentContent(
                    appRelativeUrl,
                    appRelativeUrl.Length - postTildeSlashUrlValue.Length,
                    postTildeSlashUrlValue);

                return true;
            }

            return false;
        }