ReBuildAll Blog
Thoughts (mostly) on .NET development

.NET routing and Web Part Zones == an impossible mix   (ASP.NET)   
Ever used Web Part Zones? How about ASP.NET Routing (introduced in .NET 3.5 SP1)? Both are interesting features. But try them together, and you are destined to fail. The reason for this is probably because different teams implemented them, and no one thought they would be using it together. Doh?! :-\

I bumped into this problem when I wanted to add Web Part Zone support for my rpgWiki application (a wiki we use for roleplaying). I was greeted with a nasty exception:

HttpException (0x80004005): The file '/WikiWeb/articles/Helsinki_by_Night.aspx' does not exist.

Here the /WikiWeb is the path to the application on my development server, and the rest of the path is a non existing virtual file. Without the Web Part Zone code, this works just fine, ASP.NET Routing will find the actual physical file and execute that.

Looking at what went wrong I found the following code fragment was being executed. The actual exception happened when invoking ToggleScope():

                if ( WebPartManager1.Personalization.Scope == PersonalizationScope.User )
                {
                    WebPartManager1.Personalization.ToggleScope ();
                }

In my rpgWiki project I did not want to provide per user customization. So whenever I saw the User scope, I wanted to change that to Shared scope. So why the hell do I get this exception all of a sudden?

Well, it turns out (after looking at the call stack and digging around with Reflector) that ToggleScope will change the setting, and after that execute a Server.Transfer() to the same page. In "classic" Web Forms without routing this is not a problem, but it turns out the ASP.NET team did not upgrade Server.Transfer() for routing. So it tries to execute the same address where the original request came to, and because it is a virtual path only understood by routing, it will not work.

I was able to speak with Scott Galloway from the ASP.NET team at a conference. He also confirmed this to be a bug, but couldn't say if it would be corrected.

Of course this means that not only Web Part Zones will not work, but rather Server.Transfer() will never work when you want to transfer to a page that does not exist and would need routing.



 

Comments

Jayesh Re: .NET routing and Web Part Zones == an impossible mix
Hi,

I am also stuck in the same problem. Please check the problem steps here: http://forums.asp.net/t/1642934.aspx

Kindly suggest me a solution, if any.
Lenard Gunda Re: .NET routing and Web Part Zones == an impossible mix
I had a partial solution applied.

I really only needed the WebParts in one single configuration. That is, in my wiki, all the wiki article pages would look like the same, I did not need to do different web parts for different article pages.

So I only allowed web part editing (and thus calling ToggleScope) on my default.aspx page. In other pages, editing was disabled. This solved the problem of the exception.

So then I needed a way to apply this setting to all pages. To solve this, I derived a new class from SqlPersonalizationProvider. For all methods I call the base, except for CheckPath(). Here I cheat by returning default.aspx for all my article pages, instead of the actual page. This way the Web Part system thinks I am always on the same page, and displays the configuration that I did for the default.aspx page. You have to register the personalization provider in web.config in order for this to work.

I know this is a very specific solution and might not work for everyone, but it solved the problem for me.
The Bitland Prince Re: .NET routing and Web Part Zones == an impossible mix
I got struck into the very same problem. Tried everything to make it work. Any success on your side ?

It is very frustrating that Microsoft didn't fix this yet...