Stefan Goßner

Senior Escalation Engineer for SharePoint (WSS, SPS, MOSS, SP2010) and MCMS

Blogs

How to avoid the ugly GUID based URL's

  • Comments 10
  • Likes

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>
    <% } %>
Comments
  • 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";




  • 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

  • Hi Garbriele,

    I have updated the code above.

    Cheers,
    Stefan.

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

    when using Webauthor and...

Raw Html Fix