Q: HttpContext.RewritePath

  • Thread starter Thread starter Jiho Han
  • Start date Start date
J

Jiho Han

Can someone explain in layman's term, what HttpContext.RewritePath does?
SDK doc explanation is kind of scant.
Does it only affect the request processing for the duration of the
processing(meaning server side), or is the client in anyway affected?

Thanks
 
Let say browser sent a request for /mysite/my.aspx
HttpContext.Rewrite kicks in before that aspx processed and rewrites it to
/mysite/template1/notmy.aspx.

From now on the ASP.NET engine will see that request as if came to
/mysite/template1/notmy.aspx pretty much ignoring the original request for
/mysite/my.aspx

You see??? You just rewrote the URL.

The client is unaffected in anyway (ALMOST). Since it thinks that it still
gets the HTML from /mysite/my.aspx


Guess why i have that almost there? I was amazed that practically any
article or sample of using that HttpContext.Rewrite does not mention one
small but important problem

The <form> action will be /mysite/template1/notmy.aspx
So the next time you click submit button you will clearly see in the browser
full url.
Of course there is an easy workaround "In a page_load event rewrite url back
to the original"

Another thing is you must be careful with path you specify to images or js
files.
If you use relative path browser will convert it to full path and send
request to the server. And since browser has no idea that URL was rewritten
it will ask for
/mysite/my.gif not /mysite/template1/my.gif. Which might be not what you
expected.
So it's probably better to specify the full path for images and ohter
related to the page files.


George.
 
I see. I am most interested to find out what the behavior is on the client
side.
I think now I understand why when I used HttpContext.RewritePath on my
initial post(!IsPostBack), the postback url was changed to the rewritten
path via form action attribute.
And as for non-aspx page resources, it's because those are handled by IIS
not ASP.NET, isn't it?

Bear with me one more time.
The sdk doc states: "RewritePath is used in cookieless session state." How
might RewritePath be used in a cookieless session state?

Thank you.
Jiho
 
Have you ever saw the burl of the cookieless browser?
Here is an example if you did not (Go to http://mappoint.msn.com)

It will redirect you automatically to something like
http://mappoint.msn.com/(mmagffm234v5vpmbik0oev55)/Home.aspx

This (mmagffm234v5vpmbik0oev55) is your session identifier.

I hope you do not believe that this folder (mmagffm234v5vpmbik0oev55)
actually exist on the server.

When you hit that URL asp engine rewrites url to
http://mappoint.msn.com/Home.aspx and setting correctly session object to
the one identified by (mmagffm234v5vpmbik0oev55)

You still have to watch out how do you reference images.
Since browser is not aware of those rewrites and will try to retrieve an
image from
http://mappoint.msn.com/(mmagffm234v5vpmbik0oev55)/my.gif if you used
relative path to it.


George.
 
As for embed session into the url. you will get the benefit that all relative links to other pages will automatically carryover session id.

Let say you have a page http://mappoint.msn.com/{seesionid}/mypage.aspx
and you have a link <a href="mypage2.aspx">

The sessionid will be automaticly transfered to mypage2.aspx

The overal benefit of URL rewriting is to create human readable urls.

George
My Site - Body Jewelry
Thanks George, for that example.

I've actually seen them before and I've also seen this variation:

http://mappoint.msn.com/Home.aspx/mmagffm234v5vpmbik0oev55 (Not at mappoint,
this probably won't work here)

So what is the difference between doing it this way instead of using the
querystring like this? To be precise, what benefits do you gain?

http://mappoint.msn.com/Home.aspx?sid=mmagffm234v5vpmbik0oev55

You've been most helpful.
Jiho
 
Oh I see what you mean by the session info being carried over to other pages without attaching querystring to every page.

Thanks.
As for embed session into the url. you will get the benefit that all relative links to other pages will automatically carryover session id.

Let say you have a page http://mappoint.msn.com/{seesionid}/mypage.aspx
and you have a link <a href="mypage2.aspx">

The sessionid will be automaticly transfered to mypage2.aspx

The overal benefit of URL rewriting is to create human readable urls.

George
My Site - Body Jewelry
Thanks George, for that example.

I've actually seen them before and I've also seen this variation:

http://mappoint.msn.com/Home.aspx/mmagffm234v5vpmbik0oev55 (Not at mappoint,
this probably won't work here)

So what is the difference between doing it this way instead of using the
querystring like this? To be precise, what benefits do you gain?

http://mappoint.msn.com/Home.aspx?sid=mmagffm234v5vpmbik0oev55

You've been most helpful.
Jiho
 
Back
Top