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

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
"<script language=\"javascript\" type=\"text/javascript\">\n"+
" var __CMS_PostbackForm = document."+c.ID+";\n"+
" var __CMS_CurrentUrl = \""+CmsHttpContext.Current.ChannelItem.Url+"\";\n"+
" __CMS_PostbackForm.action = __CMS_CurrentUrl;\n"+
"// -->\n"+

Comments (21)

  1. alex says:

    Hi Stefan!

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

  2. Role says:

    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?


  3. Stefan says:

    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!

  4. Role says:

    Ok. It works great now! thanks

  5. AndrewSeven says:

    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.



  6. Stefan says:

    Hi Andrew,

    I have commented your blog.



  7. Rooc says:

    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"];


  8. Stefan says:

    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….



  9. Chinye says:

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

  10. james says:

    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 ?

  11. Stefan says:

    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.



  12. mike says:

    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?

  13. Stefan says:

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

  14. Chris says:

    Hi Stefan,

    We have implemented this module, along with the SSL module, and we still seem to get the ugly URL’s when switching between secure / non-secure pages.

    eg. if you go from https – http the URL is "ugly", but if you navigate to a subsequent http page, it will correct itself. The same is also true in reverse. (http – https – https)

    any ideas?

    BTW, your site is a great help…thanks

  15. Stefan says:

    Hm… I did not test this scenario.

    How do you switch between SSL and non SSL?

    Using this approach:




  16. Chris says:

    Hi Stefan,

    Yes, we are using your ssl module.


  17. Stefan says:

    I have analyzed this problem. The reason is that that the CmsSslHttpModule does a redirect to the Url it receives with either http or https. This Url is already the internal Url as the request already has passed the MCMS ISAPI filter. This redirect will now return to the browser and show the ugly URL.

    To correct this it would be necessary to ensure that the redirected URL from the CmsSslHttpModule is a friendly URL rather than the ugly one.

    I will check if there is an easy solution for this and post it to the CmsSslHttpModule or as a separate post.

  18. Randall says:

    Hi Stefan,

    Can you please suggest the steps to implement this code.

    Means I didn’t understand where to write the code?

    Thanks for all your help.



  19. Anonymous says:

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

    when using Webauthor and…

Skip to main content