Anders Arpi
Anders Arpi

Reputation: 8397

Replacing the route resolver in ASP.NET MVC

So my MVC application gets a request from a user who has just browsed to e.g. /home/about. It checks the RouteTable for a matching pattern and dispatches it to the correct Controller and Action. Great.

What if I want to take over that function? I don't want to rewrite the URL, I don't want to mess with the RouteData property in a filter context or anything like that. I want to take over the part where route X gets translated to controller Y and action Z by my MVC application. Something like MyRouteResolver : IRouteResolver (if that actually existed).

I have been digging around on MSDN but I can't find any hint on where this is done. Any help is appreciated.

Upvotes: 2

Views: 643

Answers (1)

Betty
Betty

Reputation: 9189

You could write an http handler / module that effectively does the same thing, look at System.Web.Routing.UrlRoutingHandler to see how they do it.

One problem however, is that all the calls within MVC to find the url for a route wouldn't work (eg UrlHelper), they are all hardcoded to grab the static route collection. You could fill that route collection to make it work, but then I'm not sure what you're gaining from having your ow routing handler.

The current routing handler is fairly flexible, you maybe be able to write your own route classes (see RouteBase) instead of replacing the entire handler.

Upvotes: 2

Related Questions