Is URLRewriting what I want?

D

darrel

I'm building a site template that, hopefully, will be just one single ASPX
page.

There are 10 main districts (each getting a variation of the template), and
I could easily pass that info via a querystring:

mysite.com/default.aspx?district=3

But I'd like to be able to have the URL define that:

mysite.com/district/3

I'm looking at URL rewriting and I'm not quite sure if it does what I think
I want it do to.

It looks like URL rewriting (such as ISAPIrewrite) can take the above url:
'mysite.com/district/3' and then rewrite it as the querystring:
'mysite.com/default.aspx?district=3' which my application could use.

I was wondering if there would be a method to simply have
'mysite.com/district/3' point at /default.aspx. That way I could parse the
URL directly to get the variable rather than having to grab another
querystring. Is that something URL rewriting can do?

I COULD just do the former, but I'm trying to reduce the amount of
persistent querystrings we need to maintain.

-Darrel
 
G

George

Yes, you are looking in correct direction.

Here are steps.

1. Override Application_BeginRequest(Object sender, EventArgs e) in Global.asax
2. Save the current path request

HttpContext ctx = HttpContext.Current;

string sPath = ctx.Request.Path.ToLower();

ctx.Items["OriginalPath"] = sPath;

then parse it out the query path and do HttpContext.Current.RewritePath("~/Default.aspx", "distict=3", "" ); (see help for RewritePAth)

3. in default.aspx Page_Load rewrite it back to the path saved in Items["OriginalPath"]



PS: Pay attention to # 3 otherwise all forms will point to default.aspx and not to /district/3.

George.





I'm building a site template that, hopefully, will be just one single ASPX
page.

There are 10 main districts (each getting a variation of the template), and
I could easily pass that info via a querystring:

mysite.com/default.aspx?district=3

But I'd like to be able to have the URL define that:

mysite.com/district/3

I'm looking at URL rewriting and I'm not quite sure if it does what I think
I want it do to.

It looks like URL rewriting (such as ISAPIrewrite) can take the above url:
'mysite.com/district/3' and then rewrite it as the querystring:
'mysite.com/default.aspx?district=3' which my application could use.

I was wondering if there would be a method to simply have
'mysite.com/district/3' point at /default.aspx. That way I could parse the
URL directly to get the variable rather than having to grab another
querystring. Is that something URL rewriting can do?

I COULD just do the former, but I'm trying to reduce the amount of
persistent querystrings we need to maintain.

-Darrel
 
D

darrel

Thanks, George. I'm actually using ISAPIrewrite...not .net natively. It
doesn't quite do what I though, but realized that rewriting it as a
querystring is actually easier anyways...both for rewriting and for grabbing
it.

-Darrel
 
K

KMA

Another way is to write a custom page to handle 404s. None of the URLs that
the user keys exist, therefore all processing goes to your 404.

One problem I see is if the URL doesn't have an ASP ending (aspx, for
example) then none of your handler will be invoked. IIS just won't know who
should handle the request. Unless you have access to IIS admin, of course.
If you use shared hosting then this probably isn't the case.

The advantage of doing through custom 404 is that you can mix "real" and
"fake" pages freely.
 
D

darrel

Another way is to write a custom page to handle 404s. None of the URLs that
the user keys exist, therefore all processing goes to your 404.

We do that as well. In fact, that's what we were doing up until this point.
We figured URL Rewriting at the IIS level would be more efficient, though,
rather than having it process a page and then redirect each time.
One problem I see is if the URL doesn't have an ASP ending (aspx, for
example) then none of your handler will be invoked.

Yes, that was the other problem, and the main problem with doing
URLRewriting in .net natively. I've been holding off on using ISAPI rewrite,
but now that I've downloaded the free version and got it up and running in a
matter of minutes, I'm not sure why I didn't do this earlier (and also
wonder why MS hasn't bought them out and incorporated it into IIS natively
yet).

-Darrel
 

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

Top