Question

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?

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top