Пример #1
0
        public static string WidgetContent(this UrlHelper helper, string contentPath, string assemblyName)
        {
            if (contentPath.IsNullOrEmpty())
            {
                throw new ArgumentNullException("contentPath");
            }

            if (string.IsNullOrEmpty(assemblyName))
            {
                return(UrlHelpers.WidgetContent(helper, contentPath));
            }

            var packagesManager = new PackageManager();
            var packageName     = packagesManager.GetCurrentPackage();

            if (contentPath.StartsWith("~", StringComparison.Ordinal) || contentPath.StartsWith("/", StringComparison.Ordinal) || contentPath.Contains("://"))
            {
                var url = UrlTransformations.AppendParam(contentPath, PackageManager.PackageUrlParameterName, packageName);
                return(helper.Content(url));
            }

            var resourceUrl         = string.Format("~/{0}/{1}", FrontendManager.VirtualPathBuilder.GetVirtualPath(assemblyName), contentPath);
            var contentResolvedPath = UrlTransformations.AppendParam(resourceUrl, PackageManager.PackageUrlParameterName, packageName);

            return(helper.Content(contentResolvedPath));
        }
Пример #2
0
        /// <summary>
        /// Resolves URL based on the current widget.
        /// </summary>
        /// <param name="helper">The URL helper.</param>
        /// <param name="contentPath">The content path.</param>
        /// <returns>Resolved URL.</returns>
        /// <exception cref="System.ArgumentNullException">contentPath</exception>
        /// <exception cref="System.InvalidOperationException">
        /// Could not resolve the given URL because RouteData of the current context is null.
        /// or
        /// Could not resolve the given URL because RouteData does not contain \controller\ key.
        /// </exception>
        public static string WidgetContent(this UrlHelper helper, string contentPath)
        {
            if (contentPath.IsNullOrEmpty())
            {
                throw new ArgumentNullException("contentPath");
            }

            var packagesManager = new PackageManager();
            var packageName     = packagesManager.GetCurrentPackage();

            if (contentPath.StartsWith("~", StringComparison.Ordinal) || contentPath.StartsWith("/", StringComparison.Ordinal) || contentPath.Contains("://"))
            {
                var url = UrlTransformations.AppendParam(contentPath, PackageManager.PackageUrlParameterName, packageName);
                url = UrlHelpers.AppendVersion(url);
                return(helper.Content(url));
            }

            if (helper.RequestContext.RouteData == null)
            {
                throw new InvalidOperationException("Could not resolve the given URL because RouteData of the current context is null.");
            }

            var    contentResolvedPath = string.Empty;
            object controllerName;

            // "widgetName" is a parameter in the route of the Designer. It allows us to have a special fallback logic
            // where we first check for the requested resource in the widget assembly and then fallback to the current controller assembly.
            if (helper.RequestContext.RouteData.Values.TryGetValue("widgetName", out controllerName))
            {
                contentResolvedPath = UrlHelpers.GetResourcePath((string)controllerName, contentPath, PackageManager.PackageUrlParameterName, packageName);
            }

            if (string.IsNullOrEmpty(contentResolvedPath))
            {
                if (helper.RequestContext.RouteData.Values.TryGetValue("controller", out controllerName))
                {
                    contentResolvedPath = UrlHelpers.GetResourcePath((string)controllerName, contentPath, PackageManager.PackageUrlParameterName, packageName);
                }
                else
                {
                    throw new InvalidOperationException("Could not resolve the given URL because RouteData does not contain \"controller\" key.");
                }
            }

            if (string.IsNullOrEmpty(contentResolvedPath))
            {
                var url = "~/" + FrontendManager.VirtualPathBuilder.GetVirtualPath(typeof(UrlHelpers).Assembly) + contentPath;
                contentResolvedPath = UrlTransformations.AppendParam(url, PackageManager.PackageUrlParameterName, packageName);
            }

            contentResolvedPath = UrlHelpers.AppendVersion(contentResolvedPath);

            return(helper.Content(contentResolvedPath));
        }
Пример #3
0
        /// <summary>
        /// Generate url template that suports paging.
        /// </summary>
        /// <param name="controller">Target controller.</param>
        /// <param name="pageUrl">Base url for paging.</param>
        /// <param name="urlKeyPrefix">Query parameter sufix.</param>
        /// <returns>Url suffix.</returns>
        public static string GeneratePagingTemplate(this ContentBaseController controller, string pageUrl, string urlKeyPrefix)
        {
            if (urlKeyPrefix.IsNullOrWhitespace())
            {
                return(string.Concat(pageUrl, UrlHelpers.GetRedirectPagingUrl(), SystemManager.CurrentHttpContext.Request.QueryString.ToQueryString(true)));
            }

            var key         = string.Format(KeyFormat, urlKeyPrefix);
            var queryParams = HttpUtility.ParseQueryString(SystemManager.CurrentHttpContext.Request.Url.Query);

            queryParams[key] = "{0}";

            var queryParamsRaw = HttpUtility.UrlDecode(queryParams.ToQueryString(true));

            return(string.Concat(SystemManager.CurrentHttpContext.Request.Url.LocalPath, queryParamsRaw));
        }