context.RewritePath always ends up back at default.aspx

Mar 2, 2008 at 7:53 AM
I'll try to make this as concise as possible. I'm trying to create a 'photo gallery component' to plug into BlogEngine.net and the features I have planned seem to rule out simple 'extensions' or 'User Controls'. So I'll describe my plan of attack at a high level.

1) Have a DigitalGallery.aspx at site root similiar to Post.aspx (in that it simply has a <asp:content/> item to replace site.master's content). I currently plan on deriving from BaseBlogPage still, but that may change.

2) DigitalGallery.aspx will never be the end point of a url, it'll always be hit via Url re-writing, so I'll add a IHttpModule as well, that handles this.

So, I currently have the following 'items set':

1) Url in site.master: <asp:HyperLink ID="hlPhotos" NavigateUrl="~/dg/photos.aspx" runat="server">Photos</asp:HyperLink>
2) Added new HttpModule in web.config: <add name="DigitalGallery.UrlRewrite" type="DigitalGallery.UrlRewrite"/>
3) DigitalGallery.aspx has simply content 'test': <asp:content id="Content1" contentplaceholderid="cphBody" runat="Server"><p>Test</p></asp:content>
4) DigitalGallery.aspx.cs has a breakpoint in protected void Page_Init( object sender, EventArgs e ) { return; } to verify I am hitting my page.
5) My UrlRewrite has following code for rewriting:
if ( url.Contains( BlogSettings.Instance.FileExtension.ToUpperInvariant() ) && url.Contains( "/DG/" ) && !url.Contains( "ERROR404.ASPX" ) )
{
if ( url.EndsWith( "/PHOTOS.ASPX", StringComparison.InvariantCulture ) )
{
string photosUrl = string.Format( "{0}digitalgallery.aspx?dg.apg=1{1}", Utils.RelativeWebRoot, GetQueryString( context ) );
context.RewritePath( photosUrl, false );
}
}

As you can see, my 'goal' here is to emulate the architecture of BlogEngine.net as much as possible so that any code improvements by users (once I submit this component) will not require a 'coding framework learning curve'. Anyway, up until this point, everything works good. When I click my link for 'Photos', everything rewrites the way it is supposed to and I hit my breakpoint in DigitalGallery.aspx.cs's Page_Init(), but upon leaving that function, I 'somehow' end up back inside BlogEngine.Core.Web.HttpModules.UrlRewrite and the context.Request.RawUrl="/BlogEngine.Web/Default.aspx?" and I fall through to this case:

else if ( url.Contains( "/DEFAULT" + BlogSettings.Instance.FileExtension.ToUpperInvariant() ) )
{
RewriteDefault( context );
}

So after clicking my Photos link, instead of seeing a simple 'Test' message in the content portion, I end up back where I started (default.aspx) and I can not for the life of me figure out what code/error/whatever redirects me back to Default.aspx.

Couple 'strange' things I found while trying to track down the problem:

1) I searched for "default", "/default", "default.aspx", "location", "RelativeWebRoot", ".Redirect", ".Transfer", and ".RewritePath" all in attempt to try and figure out who might be redirecting me. I put break points on all items I thought had a chance of doing it and none of my breakpoints were hit. I may have missed something, but just letting you know what I searched for.

2) If I ran the site under IIS 5.0 (default was running under Cassini), instead of "/BlogEngine.Web/Default.aspx?", it was "/BlogEngine.Web/Default.aspx" (without the ?).

3) Just to make sure there wasn't a problem with my HttpHandler in terms of conflicting with BlogEngine.net's HttpHandler, I changed my rewrite to:

context.RewritePath( photosUrl, false );
context.RewritePath( Utils.RelativeWebRoot + "default.aspx?calendar=show", false );

Notice that I just called RewritePath() twice and set it to a 'valid url' that was called from BlogEngine.net's HttpHandler and the calendar correctly showed up and the url in
browser remained at /BlogEngine.Web/dg/photos.aspx

4) I tried changing my DigitalGallery.aspx to derive from System.Web.UI.Page instead of BlogPageBase (I had to add an OnPreInit to set the MasterPage file) and had the same results.

5) If I continued to derive from System.Web.UI.Page and got rid of the <asp:content/> and the OnPreInit handler and simply put <p>Test</p> in for the markup, it worked. The url in the browser remained at /BlogEngine.Web/dg/photos.aspx and the page was only 'Test' (obviously all the normal blogging elements were removed since I wasn't using the master file).

Sorry to be so long winded, but I didn't know of any other way to ask without most likely resulting in rash of comments 'back and forth' trying to figure out 'why I'm doing what I'm doing' and 'where I made changes' and what not. Desperately hoping someone has an idea for either:

1) How to fix my problem, or
2) A better way to implement a photo gallery component where galleries and photos can both have permanent urls, ability to rate and comment items, and ability to hook up to a RSS feed of comments on an item.

For an example of the old 'home grown' site written 'almost exclusively' with xml/xsl that I'm trying to migrate off of to get to BlogEngine.net you can see:

1) Gallery Listing: http://www.aneyfamily.com/TerryandAnn/Pictures/Page/1.aspx
2) Photo Permalink: http://www.aneyfamily.com/TerryandAnn/Pictures/50/764.aspx

Thanks in advance.