HttpHandler + Server.Execute to achieve URL Rewrite

G

Guest

I need to perform url rewriting to convert this (for example):
/blogs/feeds/popular/posts/

to this:
/blogs/feeds.aspx?type=popular&type2=posts

What I did was the following:

1. Created an http handler that parses the url and based on it will execute
another aspx page using Server.Execute

2. In the IIS I added ".*" to the ISAPI handler to be handled by .net

3. In web.config I added this so that it handles all urls coming in:

<httpHandlers>
<add verb="*" path="*/" type="MyHandler.FeedsHandler, FeedsHandler" />
</httpHandlers>

My question is: Is this a proper practice or are there any down sides to
using "server.execute" and "Setting the ISAPI handler to handle .* as .net
pages"?

Following is my handler code:

using System;
using System.Web;
using System.Configuration;

namespace MyHandler
{
/// <summary>
/// Summary description for FeedsHandler.
/// </summary>
public class FeedsHandler : IHttpHandler
{
#region Implementation of IHttpHandler
public void ProcessRequest(System.Web.HttpContext context)
{

HttpResponse objResponse = context.Response;
string myPath = context.Request.Path;

if (myPath.Contains("/feeds/"))
{
string[] mySplitPath = myPath.Split('/');

string myType2 = mySplitPath[mySplitPath.Length - 2];
string myType = mySplitPath[mySplitPath.Length - 3];

HttpContext.Current.Server.Execute("/feeds/feeds.aspx" +
"?type=" + myType + "&type2=" + myType2);
}
else
{
HttpContext.Current.Server.Execute(myPath + "Default.aspx");
}

}
public bool IsReusable
{
get
{
return false;
}
}
#endregion
}
}
 
S

Steven Cheng[MSFT]

Hi Jeeran,

Thank you for posting.

As for the ASP.NET Httphandler, it perform processing on the coming
request according to the certain document extension configured in
web.config. Howerver, I don't think it can be mapped to process pure
directory path like "http://server/app/subdir/", such directory path can
only be intercepted by raw IIS isapi extension.

Also, for ASP.NET Url Rewriting, I think you can also consider use
HttpModule, since it runs before httphandler in the ASP.NET's server-side
http pipeline. here is an MSDN article describing this:

#URL Rewriting in ASP.NET
http://msdn.microsoft.com/library/en-us/dnaspp/html/urlrewriting.asp?frame=t
rue

====================
My question is: Is this a proper practice or are there any down sides to
using "server.execute" and "Setting the ISAPI handler to handle .* as .net
pages"?
==================

IMO, I don't think it's good idea to pass all requests to ASP.NET runtime
(through the aspnet_isapi.dll extension), especially for those static
documents (like image, script file ....). Because for static files, IIS is
the best one to process them and have better performance than the ASP.NET's
static file handler.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

Jeeran

Thank you Steven.

I ended up using an isapi extension as you suggested (I used isapi_rewrite
from HeliconTech), but I did not use the isapi extension to handle every
url scenario, instead used isapi_rewrite to map the directory path to a .net
url which is then further parsed by an http handler -using the one in the
article you mentioned- and rewritten in its final shape.

My question now is; Is there any down-side to this architecture? Should we
only use either ISAPI or http handler?

As an example of what I did is the following:
1- The ISAPI rewrites this url (site.com//feeds/LANG/ITEMS/TYPE/) to (site.com/feeds/LANG/ITEMS/TYPE.aspx)
2- Then an http handler rewrites (site.com/feeds/LANG/ITEMS/TYPE.aspx) to
(site.com/feeds/feed.aspx?lang=LANG&items=ITEMS&type=TYPE)

Both using regular expressions which extract LANG and TYPE and makes it easy
to achieve all of this using just two rewrite rules, one in isapi_rewrite
and one in the http handler.

I figured this is a more flexible solution which allows:
1- Reducing the number of rules the ISAPI has to check for thus reducing
load on IIS.
2- Offloading most of the handling to the http handler will give developers
better access to further development and testing of new rules. Rather than
have to change the rules in the ISAPI filter each time.

Thank you.
 
S

Steven Cheng[MSFT]

Thank you for your followup Jeeran,

I think your currect implementation is good. And I just have one comment
about the URL rewriting in ASP.NET side, you're currently using httphandler
to do the redirecting. IMO, httpModule will be better since in the ASP.NET
runtime pipeline, httpmodule come at the earilier time to process comming
requests, so if we do the URL rewriting in httpmodule, that'll avoid the
request to pass other additional modules or processing. Here is a good
article on the ASP.NET runtime architecture which have described the
pipeline structure:

http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top