How to avoid the ugly GUID based URL's

There are two different reasons for ugly URL’s on in presentation mode:

  1. when using Webauthor and switching to live site
  2. when doing a postback in on a form to send data to the server.

There is a solution for both problems. Problem 1 requires an additional postback to the server to retrieve the page with the nice looking URL and the second problem can be avoided by adding some code to prevent the postback to the ugly URL by replacing the postback URL with the nice looking one.

Solution for Problem 1:

1) Add the following code part to global.asax.cs of you template project:


    public void Application_PreRequestHandlerExecute(Object sender, EventArgs e) 
    { 
       
// 
        // correct the url after switch to live 
        // 
        Posting thisPosting = CmsHttpContext.Current.Posting; 
        PublishingMode currentMode = CmsHttpContext.Current.Mode; 

        if (thisPosting != null && currentMode == PublishingMode.Published) 
        { 
            if ( Request.QueryString["NRORIGINALURL"] != null &&
                 Request.QueryString["NRORIGINALURL"].StartsWith("/NR/exeres") ) // oh so ugly 
            { 
                if ( !thisPosting.Url.StartsWith("/NR/exeres") )
                    Response.Redirect (thisPosting.Url); 
            } 
        } 
    }

2) Wire up this event in the global init:

    this.PreRequestHandlerExecute += new EventHandler(this.Application_PreRequestHandlerExecute);

The complete implemetation looks like this:

    namespace ...
    {
        ///
        /// Summary description for Global.
        ///
        public class Global : System.Web.HttpApplication
        {
            public Global()
            {
                InitializeComponent();
                this.PreRequestHandlerExecute +=
                      new EventHandler(this.Application_PreRequestHandlerExecute);
            }
            public void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
            {
                //
                //  correct the url after switch to live
                //
                Posting thisPosting = CmsHttpContext.Current.Posting;
                PublishingMode currentMode = CmsHttpContext.Current.Mode;
                if (thisPosting != null && currentMode == PublishingMode.Published)
                {
                    if ( Request.QueryString["NRORIGINALURL"] != null &&
                         Request.QueryString["NRORIGINALURL"].StartsWith("/NR/exeres") )  // oh so ugly
                    {
                        if ( !thisPosting.Url.StartsWith("/NR/exeres") )
Response.Redirect (thisPosting.Url); } } } ... } }

Solution for Problem 2:

Add the following code behind the tag in the template or add it to a User Web Control which you can add to all your templates:

    <% if (WebAuthorContext.Current.Mode == WebAuthorContextMode.PresentationPublished) { %>
        <SCRIPT language=javascript>
        <!--
            __CMS_PostbackForm.action =
               '<% =Microsoft.ContentManagement.Publishing.CmsHttpContext.Current.Posting.Url %>';
        // -->
        </SCRIPT>
    <% } %>

4 Comments


  1. I think I (we) got his from you some time ago.

    Thanks.

    We use a second condition right after that (so when the redirect comes back) that uses Context.RewritePath to swap out the .htm extension with aspx.

    There were pages that would continue to revert to ugly paths when we would do a postback. The culprit was a script block sent to the client so I added a little "filter" so the troublesome block doesn’t get sent to the client.

    override public void RegisterClientScriptBlock(string key,string script)

    {

    if(!(ShowCMSAuthorControls==false && key=="__CMS_Page")){

    base.RegisterClientScriptBlock(key,script); }}

    where

    showCMSAuthorControls =

    CmsHttpContext.Current.IsLoggedInAsGuest == false

    && WebAuthorContext.Current.Mode.ToString() != "AuthoringPreview"

    && WebAuthorContext.Current.Mode.ToString() != "PresentationUnpublishedPreview";

    Reply

  2. Just an observation, Stefan, about solution to problem n. 1.

    If you have more posting with the same Name in the same channel, their url will start with "/NR/exeres", leading to an infinite loop.

    I would suggest to test for this condition before redirecting.

    Cheers,

    Gabriele Cannata

    Reply

  3. Hi Garbriele,

    I have updated the code above.

    Cheers,

    Stefan.

    Reply

  4. There are two different reasons for ugly URL’s on in presentation mode:

    when using Webauthor and…

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.