By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

URL Rewriting using IHttpHandlerFactory

P: n/a
Hi,

I'm playing around with url rewriting and I have come across a problem which
I
can't seem to get past. The general ide is to have a IHttpHandlerFactory
class
which checks the incoming request url against a set of predefined rules and
creates and return the appropriate IHttpHandler implementation.

Inside the IHttpHandler implementations, the request is examined and
interpreted
in the correct way. In short they parse a request into a new url (for
example, could
also be a handler to generate an rss-feed or whatever, hence the need for
the
factory and set of rules) and rewrites it using context.RewritePath()

My problem is (verified by stepping the debugger) is that the factory
creates the
correct Handler.. and the andler interprets the request and calls
RewritePath()
but the page is not displayed in my browser, instead a blank page is shown.

Basicly this is the setup of the testsite (used to reproduce the problem) is
as
follows

Root
Test
one.aspx
two.aspx
Webform.aspx

The URL Rewriting should be able to take Root/Test/1.aspx and rewrite it
into
Root/Test/one.aspx and the same for 2/two.aspx - so it's a very basic test
setup.

The handler which is created it setup as following

// CustomHandler.vb
Public Class CustomHandler
Implements IHttpHandler

Public ReadOnly Property IsReusable() As Boolean Implements
System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property

Public Sub ProcessRequest(ByVal context As System.Web.HttpContext)
Implements System.Web.IHttpHandler.ProcessRequest

Dim TargetPage As String = _
Regex.Match(context.Request.Url.AbsolutePath, "[0-9]+\.aspx",
RegexOptions.IgnoreCase).Value

If (Not TargetPage Is Nothing) Then
Select Case TargetPage
Case "1.aspx"
context.RewritePath("~/test/one.aspx")
Case "2.aspx"
context.RewritePath("~/test/two.aspx")
End Select
End If

End Sub

End Class

The factory implementation (remember it's not bulletproof, it's a test app
to reproduce the issue at hand)

Public Class HttpRewriter
Implements IHttpHandlerFactory

Public Function GetHandler(ByVal context As System.Web.HttpContext,
ByVal requestType As String, ByVal url As String, ByVal pathTranslated As
String) As System.Web.IHttpHandler Implements
System.Web.IHttpHandlerFactory.GetHandler

Dim TargetPage As String = _
Regex.Match(context.Request.Url.AbsolutePath, "[0-9]+\.aspx",
RegexOptions.IgnoreCase).Value

If (TargetPage.Length = 0) Then
Return PageParser.GetCompiledPageInstance(url, pathTranslated,
context)
End If

Return New CustomHandler

End Function

Public Sub ReleaseHandler(ByVal handler As System.Web.IHttpHandler)
Implements System.Web.IHttpHandlerFactory.ReleaseHandler

End Sub

End Class

the factory has been added to web.config and all is fine.. When debugging I
can see that when I enter
http://localhost/myapp/test/1.aspx the factory reaches the "Return New
CustomHandler" line and execution
is shifted over to the new objects ProcessRequest method. Continuing to step
the code and I see it hits the
Case "1.aspx" in the select block, as expected and calls
context.RewritePath("~/test/one.aspx")

This is when I'm left with a blank page. I have my sucpicions, i.e there is
no compiled instance of the real
page (one.aspx) returned from my handler (and I can't since it's a sub) ..
how would .NET know how to
render my page then? I've seen this design before in the .Text blog engine
source code, but I've been
unable to get it to work.

Please remember that this is a basic test setup used to reproduce the
problem, in hope that someone can
point how the (obvious?) misstake I'm making. I need for a factory to create
different handlers based on
the requested path, so I can't do it like in a few articles I've seen where
they rewrite the path and call
GetCompiledPageInstance .. both done in the factory ..

I figure my handler is correct, but the problem is in my factory.. perhaps I
need to do something to my
handler object before I return it in the factory?

Any advice, suggestions, pointers, help etc is apprechiated! Don't worry
about the VB.NET part i code
C# as well so don't feel hindred by the code :P

Thanks!

Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.