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?

4

1 回答 1

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

您需要将其映射到抛出 404 而不是响应的页面,而不是忽略路由。希望这对其他人有帮助!

于 2013-09-09T23:52:04.460 回答