views:

26

answers:

2

Imagine a Web Forms application with routing. A clean page name like:

http://www.mywebsite.com/home

Might have an underlying of URL of:

http://www.mywebsite.com/page.aspx?id=3

If a user enters http://www.mywebsiter.com/page.aspx?id=3 into a browser, I need to redirect to http://www.mywebsite.com/home

Is this possible to do? I can't work out a way to do this as the routing engine is not executed for a physical page and in the page.aspx Page_Load method I have no way of knowing whether the URL was entered directly or was the result of a route.

+1  A: 

You can use the Page.RouteData.Values collection to detect if the page is being loaded due to routing, rather than a direct URL. That can be done in Page_Load().

If there are route data values (you would likely check for values that you would know should exist), then they are fine. If there are no route data values, the page has loaded 'directly', and you should redirect them.

Andrew Barber
A: 

Check out the IIS URL rewrite module.

You could also look at things like disabling routing for files (RouteTable.Routes.RouteExistingFiles = false;) - that could be dangerous though!

Jon
That doesn't help with what Petras wants; to prevent loading a page by the 'real' URL, basically.
Andrew Barber
@Andrew are you telling me you can't use URL rewriting to convert `/page.aspx?id=3` to `/home`?
Jon
I'm telling you it's not what the OP wants. He wants to prevent someone from loading /page.aspx?id=3, or rather *redirect* them if they do. Rewriting does not do that.
Andrew Barber
@Andrew I still fail to see your point, IIS URL Rewrite module can issue redirects too...
Jon