Welcome to TechNet Blogs Sign in | Join | Help

Ugly URLs with MCMS and ASP.NET 2.0

Today a newsgroup post reminded me that I did not yet publish the necessary changes for my Http Module that corrects the Ugly MCMS URLs caused by ASP.NET postbacks when ASP.NET 2.0 master pages are used.

The HttpModule I published earlier does not work in this scenario as it relies on the fact that a HtmlForm control exists in the root control collection.

This is not the case for ASP.NET master pages! Here you will find the HtmlForm control in a child collection of the MasterPage control.

And an additional problem occurs: with MasterPages I found that the ID of the HtmlForm control will not be the ID rendered in the html content. So we cannot retrieve the ID in the same way as with ASP.NET 1.1 or with ASP.NET 2.0 without master pages.

Thanks to the developers of ASP.NET it is now much easier to find the HtmlForm being used by ASP.NET: the javascript variable theForm is set with the required value.

This allows us to simplify the OnInit method of the HttpModule:

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

    if (currentPage.Request.UserAgent.IndexOf("Mac_PowerPC") > 0)
    {
        currentPage.RegisterClientScriptBlock("__CMS_Page","");
        currentPage.RegisterStartupScript("ResetFormActionScript""");
    }
    else
    {
        currentPage.RegisterClientScriptBlock("__CMS_Page",        // now lets register our script with the nice URL
            "<script language=\"javascript\" type=\"text/javascript\">\n"+
            "<!--\n"+
            "   var __CMS_PostbackForm = theForm;\n" +
            "   var __CMS_CurrentUrl = \"" + CmsHttpContext.Current.ChannelItem.Url + "\";\n" +
            "   __CMS_PostbackForm.action = __CMS_CurrentUrl;\n" +
            "// -->\n"+
            "</script>\n");
    }
}

A small caveat exists: this version of the httpmodule is no longer compatible with ASP.NET 1.1.

Published Friday, September 22, 2006 12:03 PM by Stefan_Gossner

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Friday, September 22, 2006 9:17 AM by George Leithead

# re: Ugly URLs with MCMS and ASP.NET 2.0

Was it my post in the news group that prompted you? :-)
Wednesday, October 04, 2006 4:18 AM by Stefan_Gossner

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi George,

indeed it was your post.

Cheers,

Stefan

Tuesday, October 10, 2006 2:17 PM by bhargava

# how to edit AuthoringSaveNewAction

hi stefan, how to inherit and override the AuthoringSaveNewAction abstract class.
Wednesday, October 11, 2006 2:31 AM by Stefan_Gossner

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Bhargava,

the comment section is only intended to be used for question or comments for the article.

Please post general MCMS questions into the public newsgroup microsoft.public.cmserver.general

I will answer them there.

Thanks,

Stefan

Monday, October 23, 2006 2:42 PM by Jennifer

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Stefan,

How would I maintain query strings and control state/view state during post backs?  I notice that it takes the form name and replaces the action with the posting Url.

Thanks,

Jennifer

Tuesday, October 24, 2006 3:55 AM by Stefan_Gossner

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Jennifer, the behaviour with this module should be the same as without it.

Monday, January 22, 2007 9:13 AM by Sandro Mastronardi

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Stefan,

I have a solution for .NET 1.1 that works without javascripts, I haven't had the time yet to make it .NET 2.0 compatible, but that must be very easy to achieve.  You can find it here: http://blogs.mastronardi.be/Sandro/2006/12/21/HowToMakeYourWebsitesInMCMSXHtmlStrict10Valid.aspx

Hope you like it, let me know your toughts.

Sandro

Friday, February 23, 2007 5:32 AM by Shyamala

# CmsApplicationContext dispose

Hi stefen,

we are creating and updating the posting from admin. if i dispose the CmsApplicationContext i am getting this error.

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

If i didn't dispose i am getting ODBC error.

what the issue?

Regards

Shyamala

Friday, February 23, 2007 5:40 AM by Stefan_Gossner

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Shyamala,

please open a support case for this. This is not a known problem.

Cheers,

Stefan

Thursday, January 31, 2008 11:30 AM by Jannik Anker

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Stefan,

How strange. I just found this little (important) fix, and couldn't understand why I couldn't make it work. That is, until I realized it only works when using currentPage.RegisterClientScriptBlock(...), not when using the more correct currentPage.ClientScript.RegisterClientScriptBlock ??? I tried putting both this.GetType() and currentPage.GetType() as first parameter, but neither worked.

Do you have any idea why this is?

Thursday, January 31, 2008 1:39 PM by Stefan_Gossner

# re: Ugly URLs with MCMS and ASP.NET 2.0

Hi Jannik,

the reason is that MCMS itself uses currentPage.RegisterClientScriptBlock to register a different script using this id. My script overwrites this script. Using the other method will not overwrite the script from MCMS but add it as additional script which will not lead to the desired results.

Cheers,

Stefan

Friday, February 01, 2008 5:47 AM by Jannik Anker

# re: Ugly URLs with MCMS and ASP.NET 2.0

Ah, okay. That makes sense - thanks!

Friday, February 22, 2008 7:37 AM by Stefan Goßner

# Code Samples

As some of you already noticed: GotDotNet is now down and the code samples previously hosted there have

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker