Stefan Goßner

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

Blogs

A more elegant solution to avoid ugly URL's with MCMS

  • Comments 25
  • Likes

Today I played around a little bit more with HttpModules and implemented a more elegant solution for the problem as discussed in my previous post.

Especially the second problem - normal postback caused by ASP.NET controls - was not properly solved as it required to do the modification on every template file. Using an HttpModule avoids this overhead.

Here is the complete implementation which addresses avoids both problems discussed in the previous article:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Microsoft.ContentManagement.Publishing;

namespace StefanG.HttpModules
{

   
public class CmsNiceUrlHttpModule : IHttpModule 
   
{
       
public void Init(HttpApplication httpApp) 
       
{
           
httpApp.PreRequestHandlerExecute +=
new EventHandler(this.OnPreRequestHandlerExecute);
       
}

       
public void Dispose() 
       
{
           
// Nothing to do.
       
}

       
public void OnPreRequestHandlerExecute(object o, EventArgs e) 
       
{
           
HttpContext ctx = ((HttpApplication)o).Context;
           
IHttpHandler handler = ctx.Handler;

           
// lets correct the ugly URLs when switching between update and published mode
           
Posting thisPosting = CmsHttpContext.Current.Posting;
           
PublishingMode currentMode = CmsHttpContext.Current.Mode;

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

           
// to correct the ugly URL problem for normal postbacks we have to register an eventhandler for the
           
// Init event of the page object. This handler then can register a better client script block as the one in 
           
// the console code

           
((System.Web.UI.Page)handler).Init += new EventHandler( this.OnInit );
        
}

       
public void OnInit(object sender, EventArgs eventArgs)
       
{
           
System.Web.UI.Page x = sender
as System.Web.UI.Page;

           
if (CmsHttpContext.Current != null) // valid context?
           
{
               
if (CmsHttpContext.Current.Channel != null) // posting or channel rendering script?
               
{
                   
if (CmsHttpContext.Current.Mode == PublishingMode.Published) // only in published mode!
                   
{
                        foreach (Control c in x.Controls) // find the form tag and get the ID
                       
{
                           
if (c is HtmlForm)
                            {
                                
// now lets register our script with the nice URL
                               
x.RegisterClientScriptBlock("__CMS_Page", 
                                        
"<script language=\"javascript\" type=\"text/javascript\">\n"+
                                        
"<!--\n"+
                                        
" var __CMS_PostbackForm = document."+c.ID+";\n"+
                                        
" var __CMS_CurrentUrl = \""+CmsHttpContext.Current.ChannelItem.Url+"\";\n"+
                                        
" __CMS_PostbackForm.action = __CMS_CurrentUrl;\n"+
                                        
"// -->\n"+
                                        
"</script>\n");
                                break;
                            
}
                        
}
                    
}
                
}
            
}
        
}
    
}
}

Comments
  • Hi Stefan!
    We are moving to MSCMS and your blog is a treasure chest for us. Thanks a lot!

  • Hi Stefan, great stuff. I have implemented the HttpModule in the Web.config. Now, when I switch to edit site there is a javascript runtime error message '__CMS_PostbackForm' is undefined. Do you have a idea, what I have made wrong?

    Thanks

  • This was a bug. :-(
    When I wrote this blog and copied the code I missed to include the "\n" chars in the script blog. The code above should work now.

    Sorry for that!

  • Ok. It works great now! thanks

  • Hi Stefan,

    I've been digging into this issue again due to some problems with pages/postings using IE on Macs.

    I'd love it if you could have a look and tell me what you think. Especially if you have ideas about multiple root channels.

    http://weblogs.asp.net/andrewseven/archive/2004/04/23/ElegantMCMSUrls.aspx

    -Andrew

  • Hi Andrew,

    I have commented your blog.

    Cheers,
    Stefan.

  • Hi Stefan,

    This works nicely but I added the current querystring (when available) to the 'nice url', so when available I get the Request.QueryString["NRORIGINALURL"] instead of the ChannelItem.Url.

    //////////////////////////////
    //create the nice url
    string niceurl = CmsHttpContext.Current.ChannelItem.Url;

    if (x.Request.QueryString["NRORIGINALURL"] != null && x.Request.QueryString["NRORIGINALURL"].Length > 0)
    {
    niceurl = x.Request.QueryString["NRORIGINALURL"];
    }

  • Hi Rooc,

    thanks for the suggestion. Only problem: NRORIGINALURL only works with a specific hotfix on SP1a. So it would only be a solution for users which use this hotfix....

    Cheers,
    Stefan.

  • Please could you tell me where I insert the above code? Is it in the template .aspx file? It is urgent. Thanx

  • I have encountered a problem with this where I seem to get random pieces of HTML appended on the end of the page after the </HTML> tag. Sometimes it happens when switching between edit mode & live mode, and sometimes just when navigating the site.

    Any idea ?

  • Hi Chinye,

    you need to create a new class library project and compile the above code into a DLL. Then add this DLL to the http module section of your web.config.

    Cheers,
    Stefan.


  • Wouldn't the most elegant solution be to actually change the action value on the form to the url of the posting?

    This technique is explained in the following MSDN article (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp)

    In short you create a customForm which inherits from HtmlForm and change the RenderAttributes method to correctly write the action tag. Unlike the article I set the action tag to the url of the posting.

    Unfortunatly, this does not work because the Console SetPageForm method requires a HtmlForm. This method is private and I can't override it.

    Do you agree that this would be the most elegant solution? Is there someother way to change the action attribute of the form tag?

  • not necessarily as it will require to use a different class for your forms. It is not transparent to your application.