2

I have the below:

    routes.MapPageRoute("RouteToPages", "{PageName}", "~/Page.aspx");

    routes.MapPageRoute("RouteToProducts", "products", "~/Products.aspx");
    routes.MapPageRoute("RouteToProduct", "product/{ProductName}", "~/Products.aspx");

Of course as you might have guessed, I can never go to /products on my website because it will automatically redirect me to ~/Page.aspx. Is there a way to fix this and allow routing to other "directories" while maintaining a dynamic page name on the root of my domain ?

Thanks!

4
  • Are you talking about subdirectories of 'product'? Commented Oct 4, 2012 at 20:53
  • No i'm talking about going to mydomain.com/products automatically takes me to ~/Page.aspx (first rule) since it considers /products as -> "{PageName}" Commented Oct 4, 2012 at 20:55
  • I understand now. I posted something to try below. Commented Oct 4, 2012 at 20:55
  • Was your problem solved? Commented Oct 4, 2012 at 22:57

3 Answers 3

1

I would normally write an HttpModule to handle this, but I would imagine that the rules should be first matching. Try this:

routes.MapPageRoute("RouteToProducts", "products", "~/Products.aspx");
routes.MapPageRoute("RouteToPages", "{PageName}", "~/Page.aspx");
routes.MapPageRoute("RouteToProduct", "product/{ProductName}", "~/Products.aspx");
Sign up to request clarification or add additional context in comments.

Comments

1

Put the routes in reverse order - most specific to lease specific. When redirecting to a route, it will search until it finds a match, then it stops.

routes.MapPageRoute("RouteToProduct", "product/{ProductName}", "~/Products.aspx");
routes.MapPageRoute("RouteToProducts", "products", "~/Products.aspx");
routes.MapPageRoute("RouteToPages", "{PageName}", "~/Page.aspx");

Comments

0

You should be able to flip the routes around

routes.MapPageRoute("RouteToProducts", "products", "~/Products.aspx");
routes.MapPageRoute("RouteToProduct", "product/{ProductName}", "~/Products.aspx");
routes.MapPageRoute("RouteToPages", "{PageName}", "~/Page.aspx");
  • /products should got to /products.aspx
  • /product/foo should got to /products.aspx
  • /foo should got to /pages.aspx

routes are first come first serve. If it makes route 1, that's the one it takes. {PageName} matches everything so naturally it will take it first

3 Comments

We need more of the same answer on this question.
my answer is more complete than yours for one and I answered within about 2 seconds of the other answers.
The same information is there; yours is just more verbose. and I think you meant 2 minutes not 2 seconds.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.