0

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.

2 Answers 2

1

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.

0

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!

4
  • That doesn't help with what Petras wants; to prevent loading a page by the 'real' URL, basically. Oct 5, 2010 at 23:59
  • @Andrew are you telling me you can't use URL rewriting to convert /page.aspx?id=3 to /home?
    – Jon
    Oct 6, 2010 at 0:02
  • 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. Oct 6, 2010 at 0:05
  • @Andrew I still fail to see your point, IIS URL Rewrite module can issue redirects too...
    – Jon
    Oct 6, 2010 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.