rewriting URL

C

Christina

Hiya!!

I am trying to do something really simple here..

I have a link, clicking on which leads to a page say MyPage.aspx.
There's a query string also passed to it. Hence its :

http://machineName/projName/MyPage.aspx?myQueryStr=abcd

Now, I dont want this to be displayed to the user. I want something
like:
http://machineName/projName/MyPage,
on anything, but NOT the querystring.

I tried the below code :

1)
In global.asax.vb :

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
Dim strCurrentPath As String
Dim strCustomPath As String

strCurrentPath = Request.Path
strCurrentPath = strCurrentPath.ToLower()
If (strCurrentPath.IndexOf("/MyPage.aspx") > -1) Then

strCustomPath = "http://machineName/projName/MyPage"

Context.RewritePath(strCustomPath)
End If

The above code gives an error : Invalid file name for monitoring..

2)
In page_load of mypage.aspx
Context.RewritePath(strCustomPath)

Still the same error :-(

Any pointers ?

Thanks!!
 
M

Mark Fitzpatrick

ASP.Net 2.0 has this built right in. What you'll want is the urlmapping
section of the config file (if I remember correctly). There's also a really
nice utility at http://www.urlrewriting.net that can do more extensive
mapping with regular expressions.
 
K

kferron

I don't think this is what she is asking, although urlmapping would
work for say, http://machineName/projName/MyPage.aspx (if you want
asp.net to handle files with no extension, this is a webserver setting
that has to handle the request first), you could map:


http://machineName/projName/MyPage.aspx

to

http://machineName/projName/MyPage.aspx?myQueryStr=abcd

you could not say, have MyPage.aspx also map to:

http://machineName/projName/MyPage.aspx?myQueryStr=1234

so the issue is moot.

Basically you could

a) have a different url mapping for every possible querystring
possibility (eg. MyPageAbcd.aspx -> MyPage.aspx?myQueryStr=abcd)

b) create a httphandler that takes advantage of Request.RewritePath()
that allows you to parse a request and rewrite the url to the correct
querystring (eg. http://machineName/projName/MyPage/abcd.aspx) and then
map that in your handler to ->
http://machineName/projName/MyPage.aspx?myQueryStr=abcd by parsing
http://machineName/projName/<aspxpage>/<querystringvalue>.aspx. This
option is really limitless, as long as you know how to parse the
incoming url.

c) your third option isn't as flexible, but requires no url rewriting,
and that is to post all of your data instead of passing in querystring



~kcf
 
C

Christina

Hmmm.... doesnt seem to be as simple as I thought it would be.

As kferron said, I'll focus on : httphandler -> rewrite path ->
parsing..

Meanwhile, if anybody has a link which shows how to do this, or a code
snippet, that would be great!!

Thanks for the help!!
 
C

Christina

Hey, I would like to consider the third approach also:
post all of your data instead of passing in querystring

Can I get a pointer to that too..

Thanx!!
 
C

Christina

I tried to used HTTPModule:

In the Application_BeginRequest, after checkin the URL, ie if its
/myFolder/URL.aspx, then i am using
HttpContext.Current.RewritePath("/myFolder/newURL.aspx") to rewrite the
URL.

Now, this is the result:
I type http://MachineName/myFolder/URL.apx,
it displays the page - newURL.aspx (but in the addressbar, its
http://MachineName/myFolder/URL.apx).

I wanted it to be other way round, ie
I type http://MachineName/myFolder/URL.apx,
it displays the page - URL.aspx (but changes the url in the addressbar
to - http://MachineName/myFolder/newURL.apx).

The reason is, my orignal link has a querystring attached to it. Now I
dont want the user to see the query string. So I want the page to be
displayed as it would have (with the querystring), but want to display
the URL without the querystring. I wanted to rewrite just my Url (in
the addressbar) to the same url(but without query string).

Any idea ??

Thanks!!
 
H

Holger Boskugel

Hello Christina
In the Application_BeginRequest, after checkin the URL, ie if its
/myFolder/URL.aspx, then i am using
HttpContext.Current.RewritePath("/myFolder/newURL.aspx") to rewrite the
URL.

Now, this is the result:
I type http://MachineName/myFolder/URL.apx,
it displays the page - newURL.aspx (but in the addressbar, its
http://MachineName/myFolder/URL.apx).

I wanted it to be other way round, ie
I type http://MachineName/myFolder/URL.apx,
it displays the page - URL.aspx (but changes the url in the addressbar
to - http://MachineName/myFolder/newURL.apx).

The reason is, my orignal link has a querystring attached to it. Now I
dont want the user to see the query string. So I want the page to be
displayed as it would have (with the querystring), but want to display
the URL without the querystring. I wanted to rewrite just my Url (in
the addressbar) to the same url(but without query string).

Any idea ??

put the Query data in you session an make a redirect to the URL you
want and then pick up the Query from your Session.


Regards

Holger
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top