HttpContext.RewritePath - Form action changed

  • Thread starter Thread starter Steven Nagy
  • Start date Start date
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
 
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.
 
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
 
Back
Top