0

If I have the following routes, each represented as a string:-

/account/index
/account  <-- which actually goes to /account/index

and these have been registered in my global.asax ... is there any way I can convert this into a Route object so I can then do this...

var routeName = FigureOutTheRegisteredRoute("/account");
return RedirectToRoute(routeName);

Cheers :)

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

3 Answers3

2

You might find some ideas in Phil Haack's route debugger - check out DebugHttpHandler.cs in particular...

Jon
  • 16,212
  • 8
  • 50
  • 62
  • i think the main magic with @haacked code would be this line: `var vpd = RouteTable.Routes.GetVirtualPath(RequestContext, rvalues);` .. but that would mean i would need to `new` up a `RequestContext`? Which means things could get real messy, quickly? – Pure.Krome Feb 27 '11 at 13:16
0

Something as below - using RouteBase.

    RouteData rd = new RouteData(this, new MvcRouteHandler());
    rd.Values.Add("controller", controllername);
    rd.Values.Add("action", actionname);
    rd.Values.Add("url", url);
    return rd

url-manipulation-implementing-routebase

EDIT

You can use url-routing-debugger to QUERY routable .. to see which registered route matches my url-string

use Nuget Package Manager Console - install-package routedebugger

you can query RouteCollection. partial basic code as below

in

protected void Application_Start()
{
RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);

and

public static class RouteDebugger
{
    public static void RewriteRoutesForTesting(RouteCollection routes)
    {
        using (routes.GetReadLock())
        {
            bool foundDebugRoute = false;
            foreach (RouteBase routeBase in routes)
            {
                Route route = routeBase as Route;
                if (route != null)
                {
                    route.RouteHandler = new DebugRouteHandler();
                }

                if (route == DebugRoute.Singleton)
                    foundDebugRoute = true;

            }
            if (!foundDebugRoute)
            {
                routes.Add(DebugRoute.Singleton);
            }
        }


    }

}
swapneel
  • 3,061
  • 1
  • 25
  • 32
  • nope. I don't want to add a new route to the routing table. I want to QUERY the routable .. to see which registered route matches my url-string. – Pure.Krome Feb 27 '11 at 12:53
  • @Pure.Krome - chekc out my edit also on above link i have added url-manipulation-implementing-routebase for Phill's route debug link under references – swapneel Feb 27 '11 at 13:14
-1

Sorry I don't fully understand why you would want to do that. ;-(

If you don't want to register multiple routes manually in global.asax then maybe want you want is to declare routes on your controllers using attributes? Check this answer: ASP.NET MVC Routing Via Method Attributes

Sorry if that's not what you're looking for...

Community
  • 1
  • 1
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • Nope - that's not what i'm trying to do. Actually, it's the complete opposite. I'm trying to convert a string to it's corresponding registered route. – Pure.Krome Feb 27 '11 at 12:39