Welcome to TechNet Blogs Sign in | Join | Help

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
                               
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;
                            
}
                        
}
                    
}
                
}
            
}
        
}
    
}
}

Published Friday, April 23, 2004 4:10 AM by Stefan_Gossner
Filed under: ,

Comments

Friday, April 23, 2004 3:23 PM by Alex

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

Hi Stefan!
We are moving to MSCMS and your blog is a treasure chest for us. Thanks a lot!
Friday, April 23, 2004 5:04 PM by Role

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

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
Friday, April 23, 2004 6:38 PM by Stefan

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

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!
Friday, April 23, 2004 7:13 PM by Role

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

Ok. It works great now! thanks
Friday, April 23, 2004 10:46 PM by AndrewSeven

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

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
Friday, April 23, 2004 11:02 PM by Stefan

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

Hi Andrew,

I have commented your blog.

Cheers,
Stefan.
Tuesday, April 27, 2004 8:43 AM by Rooc

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

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"];
}
Tuesday, April 27, 2004 9:07 AM by Stefan

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

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.
Wednesday, April 28, 2004 7:37 PM by Stefan Go

# Solving the

Saturday, May 01, 2004 2:45 AM by Stefan Go

# Solving the

Tuesday, May 04, 2004 12:55 PM by Chinye

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

Please could you tell me where I insert the above code? Is it in the template .aspx file? It is urgent. Thanx
Friday, June 04, 2004 9:17 PM by James

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

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 ?
Sunday, June 06, 2004 11:47 AM by Stefan

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

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.
Tuesday, June 08, 2004 5:28 PM by Mike

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


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?

Tuesday, June 08, 2004 5:35 PM by Stefan

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

not necessarily as it will require to use a different class for your forms. It is not transparent to your application.
Saturday, June 12, 2004 7:55 PM by Chris

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

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
Sunday, June 13, 2004 1:18 PM by Stefan

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

Hm... I did not test this scenario.
How do you switch between SSL and non SSL?
Using this approach:
http://blogs.msdn.com/stefan_gossner/archive/2004/04/22/118024.aspx

Cheers,
Stefan.
Sunday, June 13, 2004 6:46 PM by Chris

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

Hi Stefan,

Yes, we are using your ssl module.

Chris
Monday, June 14, 2004 11:45 AM by Stefan

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

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.
Monday, June 14, 2004 12:47 PM by Stefan Go

# Update for the MCMS SSL Http Module

Saturday, July 03, 2004 7:58 AM by Stefan Go

# Update for the MCMS SSL Http Module (last updated: July 3rd, 2004)

Tuesday, August 10, 2004 10:20 PM by Randall

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

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.

Regards,
Randall
Wednesday, May 11, 2005 6:26 AM by Stefan Goßner

# MCMS tip of the day - how to avoid ugly URLs in MCMS

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

when using Webauthor and...
Wednesday, February 08, 2006 8:36 PM by Axionic Blog » Blog Archive » Resolving Ugly URL’s in MCMS

# Axionic Blog &raquo; Blog Archive &raquo; Resolving Ugly URL&#8217;s in MCMS

Wednesday, January 21, 2009 11:08 PM by Long Url issue with MCMS Websites ... | keyongtech

# Long Url issue with MCMS Websites ... | keyongtech

New Comments to this post are disabled
 
Page view tracker