简体   繁体   中英

How to get RouteCollection from a request in ASP.NET MVC 4

This is my global.asax (Registering the route)

  routes.MapRoute("NewDatabase",
             "Server/{serverId}/Instances/{instanceId}/NewDatabase/",
             new { controller = "Server", action = "NewDatabase" }
             );

            routes.MapRoute(
           "Instance",
           "Server/{id}/Instances/{instanceId}/Databases",
           new { controller = "Server", action = "Databases", id = "id",instanceId="instanceId" }
           );

            routes.MapRoute(
           "Database",
           "Server/{id}/Instances",
           new { controller = "Server", action = "Instances", id = "id" }
           );

If xyz.com/Server/12/Instance/1/NewDatabase will be the reuested URL to server, Server/{serverId}/Instances/{instanceId}/NewDatabase/ will be the matching pattern. How can I know which entry is matched for the above request ?

Thanks

RouteTable.Routes.GetRouteData(HttpContextBase)

Install the RouteDebugger nuget package and enabled it in your web.config. This tells you which routes can be hit and why.

<add key="RouteDebugger:Enabled" value="true" />

All you have to do is navigate to your url. RouteDebugger will add route info to the bottom of your page.

I am not sure if this is what you are looking for but you can create a custom route constraint which enables you to prevent a route from being matched unless some custom condition is matched.

You can implement Match method which returns a Boolean value. If you return false, the route associated with the constraint won't match the browser request. This method has RouteValueDictionary so you can check route values from there.

see this url for more information - http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM