HttpContext.RewritePath - Form action changed

S

Steven Nagy

Hi all,

I have the following file:
~/cms/page.aspx

Normally, if you hit a url like http://localhost/MyApp/cms/page.aspx?PageID=32
... this would load content dynamically. No big deal, this is common
right?

So now I want to do some URL rewritting. I want to map:

http://localhost/MyApp/Test.aspx
to
http://localhost/MyApp/cms/page.aspx?PageID=32

(important to note that these are not in the same virtual folder)

So in "global.asax.cs" I write some code in "Application_BeginRequest"
to check if "Test.aspx" was requested, and if it was, rewrite it as
"cms/page.aspx?PageID=32". This also works fine.

The problem I am finding is that the page gets written with form tags
with action="page.aspx?PageID=32"

This of course is running under root so when it POSTS BACK, it
requests:
http://localhost/MyApp/page.aspx?PageID=32
not
http://localhost/MyApp/cms/page.aspx?PageID=32

... and I get an exception (resource not found).

So how can I make the form still use the original "fake" URL
(test.aspx) which would rewrite again on the post back and get the
real file?
Or is there another solution?

Thanks,
Steven
 
G

George Ter-Saakov

Known problem with URL rewriting.
Not sure why but all articles I saw about url rewriting completely forget to
mention that. I guess speaks a lot about experience of those who wrote those
articles :)

solution rather simple. Rewrite it back first thing in a form Init method.

Here is how i do it (pseudo code)

_BeginRequest event
HttpContext.Items["originalrequest"] = Request.RawUrl;
HttpContext.UrlRewrite("my_new_path.aspx")
.....



Form_OnInit()
string sOriginalUrl = (string)HttpContext.Items["originalrequest"];
HttpContext.UrlRewrite("sOriginalUrl ")

....


George.
 
S

Steven Nagy

Hi George,

Thanks for the response.
I'm at home now and I needed this for work.
I'll try it out on Monday and repost if still having problems.

Cheers,
Steven
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top