By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

URL Rewriting - problems with postbacks

P: n/a
Hi All,

I am experimenting with URL Rewriting using the techniques outlined by Scott
Mitchell in his article "URL Rewriting in ASP.NET"
(http://msdn.microsoft.com/library/de...-us/dnaspp/htm
l/urlrewriting.asp).

In the article he mentions a problem with postbacks: "If the URLs you are
rewriting contain a server-side Web Form and perform postbacks, when the
form posts back, the underlying URL will be used". His solution is to use a
custom ActionlessForm derived from System.Web.UI.HtmlControls.HtmlForm, but
with the "action" attribute removed.

An alternative solution is outlined by Scott Allen in his blog entry "The
Passion and the Fury of URL Rewriting"
(http://odetocode.com/Blogs/scott/arc...9/22/509.aspx). He proposes
simply adding the following to Page_Load:
Context.RewritePath(Request.RawUrl);

However in my experimenting, it seems that neither of these solutions works
in one particular situation, and I'm wondering if anyone out there can help
out.

The situation is:
Redirect from: ~/products/
Redirect to: ~/Somedirectory/Somepage.aspx
Note that the "Redirect from" url does *not* contain a page name.

I have got this working fine for the first time the page is hit, having
created a /products sub-folder containing an empty Default.aspx page.

The problem is, when Somepage.aspx posts back to itself, using Scott Allen's
technique described above, the URL is rewritten *not* to ~/products/ (which
is what I want), but to ~/products/Default.aspx. While this is better than
the URL being rewritten to ~/Somedirectory/Somepage.aspx on postback, it
still isn't perfect!

And using Scott Mitchell's ActionlessForm, I get: "HTTP 405 - Resource not
allowed, The page you are looking for cannot be displayed because the page
address is incorrect". This is quite a long way from perfect.

Can anyone help?

TIA,

JON

Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Jon:

Here is what I think is happening.

When the browser requests the ~/products/ directory, IIS sees the
request is for a directory, checks the settings for the directory and
moves the request to the default document (default.aspx) since the
file exists on the file system. ASP.NET actually does see the URL as
~/products/default.aspx, because IIS has moved the request to this
location all by itself.

There are ways to get the original request (for the products
directory), which would be to make all requests go through the asp.net
runtime (enter a * in the script mapping). Then you really could
redirect from ~/products/ to /somedirectory/somepage.aspx, the problem
is I'm not sure how to get the action tag set correctly. You might
need to do something along the lines of:
http://weblogs.asp.net/jezell/archiv.../15/90045.aspx
to pull this off.

I hope to get some time to experiment with this interesting problem
this weekend.

--
Scott
http://www.OdeToCode.com/

On Fri, 8 Oct 2004 17:46:27 +0100, "Jon Maz" <jo****@surfeuNOSPAM.de>
wrote:
Hi All,

I am experimenting with URL Rewriting using the techniques outlined by Scott
Mitchell in his article "URL Rewriting in ASP.NET"
(http://msdn.microsoft.com/library/de...-us/dnaspp/htm
l/urlrewriting.asp).

In the article he mentions a problem with postbacks: "If the URLs you are
rewriting contain a server-side Web Form and perform postbacks, when the
form posts back, the underlying URL will be used". His solution is to use a
custom ActionlessForm derived from System.Web.UI.HtmlControls.HtmlForm, but
with the "action" attribute removed.

An alternative solution is outlined by Scott Allen in his blog entry "The
Passion and the Fury of URL Rewriting"
(http://odetocode.com/Blogs/scott/arc...9/22/509.aspx). He proposes
simply adding the following to Page_Load:
Context.RewritePath(Request.RawUrl);

However in my experimenting, it seems that neither of these solutions works
in one particular situation, and I'm wondering if anyone out there can help
out.

The situation is:
Redirect from: ~/products/
Redirect to: ~/Somedirectory/Somepage.aspx
Note that the "Redirect from" url does *not* contain a page name.

I have got this working fine for the first time the page is hit, having
created a /products sub-folder containing an empty Default.aspx page.

The problem is, when Somepage.aspx posts back to itself, using Scott Allen's
technique described above, the URL is rewritten *not* to ~/products/ (which
is what I want), but to ~/products/Default.aspx. While this is better than
the URL being rewritten to ~/Somedirectory/Somepage.aspx on postback, it
still isn't perfect!

And using Scott Mitchell's ActionlessForm, I get: "HTTP 405 - Resource not
allowed, The page you are looking for cannot be displayed because the page
address is incorrect". This is quite a long way from perfect.

Can anyone help?

TIA,

JON


Jul 21 '05 #2

P: n/a
Hi Scott,

Glad to know you're on the case! Looking forward to seeing what you come up
with...

Cheers,

JON
Jul 21 '05 #3

P: n/a
Well, I fiddled around with the second RewritePath and I did not come
up with a way around the problem. I think the solution will involve
overriding the HtmlForm class methods. I'll keep plugging away at this
when I get a chance but I'm afraid it's not going to be an elegant
solution I was hoping to find!
--
Scott
http://www.OdeToCode.com/

On Mon, 11 Oct 2004 10:14:57 +0100, "Jon Maz" <jo****@surfeuNOSPAM.de>
wrote:
Hi Scott,

Glad to know you're on the case! Looking forward to seeing what you come up
with...

Cheers,

JON


Jul 21 '05 #4

P: n/a
Hi Scott,

An inelegant solution is a lot better than no solution at all! Good to know
I'm not alone with this problem...

Thanks for the help,

JON
Jul 21 '05 #5

P: n/a
pm
http://www.alistapart.com/articles/footers/

"Jon Maz" wrote:
Hi Scott,

Glad to know you're on the case! Looking forward to seeing what you come up
with...

Cheers,

JON

Jul 21 '05 #6

P: n/a
Did you mean this article: http://www.alistapart.com/articles/urls/ ?

If so, the article discusses Apache's mod_rewrite, and has only this to say
about IIS: "Unfortunately IIS does not come with a rewrite mechanism. You
can write an ISAPI filter to do this for you."

J
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.775 / Virus Database: 522 - Release Date: 08/10/2004
Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.