0

I have a project in VS 2015. There is a page called home-program.aspx.

This is the route I set up for that page:

            Dim sDestinationRegExp As String = "^(world|land|line|run|club)${2,}"

            routes.MapPageRoute("landing", "destinations/{destination}", "~/home-program.aspx",
                                        True,
                                        New RouteValueDictionary(New With
                                            {.destination = "world"}),
                                        New RouteValueDictionary(New With
                                            {.destination = sDestinationRegExp}))

Now, the route works. However. the user is still able to access that page by using /home-program or /home-program.aspx. Is there a way to prevent them from accessing those pages using those urls and instead use the mapped route instead?

pmb88
  • 147
  • 2
  • 11

1 Answers1

0

I suspect you can add a "catch-all" route at the tail-end of the route mapping that will map everything to a 403 or 404 page (whatever you think is best).

This answer shows how to create a catch-all route mapping. Make sure it is at the end of the maps -- the last thing you add.

This should prevent the rest of the pipeline modules from trying to process the URLs. Another IIS module (from ASP.NET) will get invoked by default if your route maps don't match a URL. This is how the request for /home-program.aspx gets handled. If you handle it with a route map entry, the default module shouldn't interfere.

Community
  • 1
  • 1
Alan McBee
  • 4,202
  • 3
  • 33
  • 38
  • The catch all does not work. The catch all only applies to the paths and pages that don't exist. The paths I want to restrict do exist. I only want a user to go to home-program.aspx by using the mapped route only and not using home-program.aspx in the browser – pmb88 Dec 17 '15 at 06:26
  • Did you try it? Route tables process all requests, and depending on how you have IIS set up, might even process non ASP.NET requests (like those that get processed by the static file handlers. In fact, when programmers *want* to have .aspx pages invoked (or, more frequently, .axd handlers), they have to add an IgnoreRoute mapping, like `routes.IgnoreRoute(“{resource}.aspx/{*pathInfo}”);` – Alan McBee Dec 17 '15 at 06:47
  • I tried it again and it does not work. The catch all would only apply to paths and files that don't exist and that also do not match pathed routes, What I am asking for is that when someone uses home-program.aspx in the browser that it does not allow them to load the page and force the user to use the mapped path to access the page. – pmb88 Dec 17 '15 at 22:44
  • Give me a few minutes, I'll see what I can do to either refine or retract my answer. – Alan McBee Dec 17 '15 at 22:53
  • I think I might have figured out a solution. I guess my initial query if there was an web config setting to restrict access to the physical file and only be able to access it via a mapped route. What I think I'll do is check the rawurl and if the string matches that of the physical file name of the page do a 301 redirect to the default page. That way the only they could get on that page is using the mapped path. I apologize if I sounded testy. – pmb88 Dec 19 '15 at 03:09
  • I didn't think you were being testy. It turns out that I could not find a simple solution to this. I even tried adding a HttpNotFoundHandler mapping in the web.config, to no avail. The only solution that I knew would work isn't exactly simple -- it's just intercepting the BeginRequest event in the Application, or using a HttpModule and handling the ProcessRequest event -- to cause 301 or 404 or whatever when an .aspx path is requested. Hardly elegant, and doesn't use URL routing maps at all. Maybe you should answer your own question, but if I do find a way, I'll edit this answer, too. – Alan McBee Dec 19 '15 at 21:33