Querystring Values

R

Roshawn Dawson

Hi,

While browsing the web, I've noticed that a lot of websites have values
in the url without any file extensions mentioned. Consider this (fake) url:

http://www.yourdomain.com/article/1

It seems that the "1" is actually a querystring value. I always thought
that you must first specify a file with a valid extension and then
append the querystring value, like this:

http://www.yourdomain.com/article/somepage.aspx?id=1

I'd sure like to get my urls to appear like the first one. Is there any
method that can be used to get the desired result in ASP.NET,
preferrably without too-too much hasle?

Thanks,
Roshawn
 
J

jasonkester

Often, a site will have a filter sitting in front of the webserver that
intercepts requests and reformats them. For IIS, this is often done
with ISAPI.

Basically, you'll write a little C++ or Perl program that takes strings
like #1 below, and turns them into #2 below. This was pretty popular
back in the day, but has fallen out of favor lately. Probably because
ASP.NET doesn't do it for you, and you can't write ISAPI filters in C#.

1. http://www.yourdomain.com/article/1 and
2. http://www.yourdomain.com/article/somepage.aspx?id=1


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
R

Roshawn Dawson

Thanks. I sure don't like using ISAPI filters, since I wouldn't have
physical access to the production web server.

However, isn't it possible to achieve the same thing using either an
httphandler or httpmodule?

Roshawn
 
J

jasonkester

You can also pull it off with a custom 404 handler that looks at the
url the user was trying to get to, then reformats and redirects if it
matches a pattern. That's how you usually end up doing this in
ASP.NET.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

Alan Silver

The example you showed would probably have been run through an ISAPI
filter as others have mentioned. That allows you to do pretty much what
you like with the incoming URL and send the request on to another URL,
including querystring values as you wish. URLReplace (by a Czec fellow
whose name escapes me at the moment) is a well known and free ISAPI
filter that does just this.

You can do the same in ASP.NET if you are prepared to live with an .aspx
ending on the URLs. Thus you can have ...

http://www.yourdomain.com/article/1.aspx

and have it mapped invisibly to ...

http://www.yourdomain.com/article.aspx?articleid=1

You need to write code in Application_BeginRequest to grab the
Request.Path and parse it for the info you want. Build the real URL,
including the querystring and then use Context.RewritePath to send the
request on to that URL.

You don't actually need HttpHandlers and so on. I have no idea why all
the examples of URL rewriting use them. It took me ages of confusion
before I discovered how simple URL rewriting really is.

Hope this helps.
 

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