3

I am using routing in webforms to clean up urls. I store all my .aspx files in the /Pages directory and route to them with clean URLs. As it stands I can still access these files by navigating to /Pages/Home.aspx for example. How can I either throw a 404 or redirect to a different page in my routing table? Here is what I have tried:

routes.RouteExistingFiles = true;
routes.Ignore("{resource}.aspx/{*pathInfo}");
routes.Ignore(@"*\.aspx");

routes.MapPageRoute("Home", "", "~/Pages/Home.aspx");

This isn't working at all, and I could use some advice.

If I try accessing a physical file on my MVC site, I get the following:

Object reference not set to an instance of an object.

Can I do this on webforms?

Zerkey
  • 795
  • 1
  • 6
  • 16
  • I got that line from documentation. That line enables routing on files that already exist on disk. – Zerkey Sep 09 '13 at 22:00
  • It defaults to false, allowing direct access to files when the URL is correct. In the course of trying to disallow file access, I set it to true so my routing procedures could handle all incoming requests. Didn't change a thing but I left it in the code. – Zerkey Sep 09 '13 at 22:12
  • Sorry - you are right - it is opposite of what was remembering. Need to read docs myself :). I believe the issue than `Ignore(@"*\.aspx");` - you explicitly request to not route all `aspx` files. You also need some route that matches "pages/{name}.aspx" urls (which is where your 404 reporting should be) – Alexei Levenkov Sep 09 '13 at 22:38
  • That line actually ended up being key. I had removed it because of your previous comments and was exploring the last part of your suggestion, which made perfect sense but wasn't working. Added the line back in, and bingo! – Zerkey Sep 09 '13 at 23:40

1 Answers1

0
routes.RouteExistingFiles = true;
routes.MapPageRoute("Route1", "Pages/{page}.aspx", "~/404.aspx");
routes.MapPageRoute("Route2", "Pages/{folder}/{page}.aspx", "~/404.aspx");

Instead of Ignoring the route, you need to map it to a page that throws a 404 instead of a response. Hopefully this helps someone else!

Zerkey
  • 795
  • 1
  • 6
  • 16