SaaS CMS has officially launched! Learn more now.

MVC: How to correctly resolve Url property in Controller?

Vote:
 

Hello,

When the property URL is used to link to a "Page" (i.e. not external URL), the value of that URL property becomes something like {/link/4320a14625af4d7c84633d85d88e8e7c.aspx?id=1234}.

In an MVC view, I can use Url.ContentUrl to resolve this URL to a nice friendly URL (/en/my-page/).

But in some cases I need to resolve the URL in a Controller, so I use UrlResolver.Current.GetUrl. Howerver, the resulting friendly url still has the internal ID appended as a querystring (the internal URL above would get resolved to /en/my-page/?id=1234).

How do I resolve the Url property in a controller without getting the internal ID querystring?

/John

#112324
Oct 28, 2014 8:10
Vote:
 

Hi

The reason id parameter is left is that the plan is to remove the "classic" format (the one with id queryparameter). Then you as partner can use the id parameter for your own purposes. Therefore we didn't want to expose the method to convert from classic format on urlresolver (since the plan is to remove it).

So what you can do now is to have an own method like below (the above code is similar as an internal method on urlresolver)

        /// <summary>
        /// Converts from a link with a classic (/template.aspx?id=123) format to a friendly URL.
        /// </summary>
        /// <param name="classicUrl">The permanent URL.</param>
        /// <returns>A URL in string representation.</returns>
        internal virtual string ConvertClassicToContentUrl(string classicUrl)
        {
            var publicUrl = UrlResolver.Current.GetUrl(classicUrl);
            if (!String.IsNullOrEmpty(publicUrl))
            {
                //remove "known" query parameters
                var builder = new UrlBuilder(publicUrl);
                builder.QueryCollection.Remove("id");
                builder.QueryCollection.Remove("epslanguage");
                return (string)builder;
            }
            return classicUrl;
        }
#112325
Oct 28, 2014 8:37
Vote:
 

Thanks for the clarification and suggestion! Much appreciated.

#112370
Oct 28, 2014 9:57
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.