Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forward slashes as querystring? HttpContext.Current.RewritePath? 1

Status
Not open for further replies.

ifx

Technical User
Feb 26, 2002
57
GB
Hi,

I've written a page which takes a number of variables as querystrings, eg:

mypage.aspx?id=123&action=show

What I'd like to do, instead of using the string above, is use a sort of file structure look (ie separate variables by a '/') such as:

mypage.aspx/123/show

Or even better:

mypage/123/show

I know this is possible, because I have seen it done. I've done some research into it and it looks like it has something to do with HttpContext.Current.RewritePath() within the Global.asax file.
I set that to mypage.aspx in the Application_BeginRequest Sub of Global.asax like:
HttpContext.Current.RewritePath("mypage.aspx")
and it redirects every request for an aspx file (existent or not) to mypage.aspx - so I'm pretty sure I'm getting close!

Any help on finishing the next bit would be brilliant! Thanks!
 
Sure - even after you use RewritePath, Server.Transfer or whatever, you can still access the originally requested URL. Then you could split that URL into an array on the "/" character and you'd have your arguments.

There is some of this type of action used in the GotCommunityNet community starter kit, if you can find that - I think it's on the GotDotNet community thing. You can download the source code. They use an HttpModule to handle the BeginRequest event, but you could use the Global.asax just as well.

 
Here's something that might work.

In the BeginRequest event, first split the url into an array, something like this
Code:
string[] args = Request.Url.ToString().Split('/');

then add that array to the Context, and Transfer to the actual page you want to execute.
Code:
HttpContext.Current.Items.Add("ArgumentArray", args);

Server.Transfer("mypage.aspx");

Now, in your Page_Load event on MyPage.aspx, retrieve the string array from the context and pick out the values.
Code:
string[] args = (string[])Context.Items["ArgumentArray"];
for(int x=0; x<args.Length; x++){
   string arg = args[x];
   //etc, etc, etc
}

Sorry this code is in C#, but you get the idea. (Plus it's typed w/o intellisense).

Good luck. ;)
 
Cheers! That's got me on the right track now, and I seem to be getting close to my solution. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top