2

Was looking at asp.net mvc complex routing for tree path as an example of how to define a custom route handler for my MVC app. Essentially, I want to give the end user ultimate flexibility in defining the URL for any given page, so I provide them with a field in the interface to specify their own custom URL.

My custom route handler is basically a wild-card handler. It will do a lookup and if it finds a match, map it accordingly. However, if no match is found, I want it to fall back and find the next rule that matches in the global.asax. Is that possible? Or do I essentially need to code the mappings that used to exist in my global.asax into my custom route handler?

Community
  • 1
  • 1
Sam
  • 9,933
  • 12
  • 68
  • 104

2 Answers2

1

Routing already works this way

When a certain route definition doesn't match request's URL, routing skips to next route definition and so on and so forth. Until:

  • it finds a route that mathes request URL
  • fails with the last route definition throwing a 404

Provide an example of how your routing should work and we'll easier give you information whether custom route handler or route is the way to go in your case.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Robert, essentially there are no rules for how the end user creates their URL. They may be migrating from another system and want to keep their old URLs, may want a short sweet url for sharing/promotional, etc. So essentially, I need to have a wildcard mapping that catches anything other than the home page. But now that you mention it, maybe I should just put in the wildcard route after my standard routes. This probably satisfies 99% of the cases. – Sam Mar 14 '11 at 22:29
  • 1
    @Sam: I don't know (you should) but if you need greedy segment anywhere in route URL definition I've written a class that supports just that. Check my blog: http://erraticdev.blogspot.com/2011/01/custom-aspnet-mvc-route-class-with.html Just in case you need something like that. – Robert Koritnik Mar 15 '11 at 08:16
  • @Sam: you're welcome. Always glad to hear that my mumblings are useful to others. – Robert Koritnik Mar 16 '11 at 19:12
1

You should be able to achieve this pretty simply.

Just have a catch all route that sits below your more specific routes, handle the catch all in your controller.

I guess you will just look for a page key then return the page to the user.

David McLean
  • 1,462
  • 1
  • 12
  • 27