Note that there are some explanatory texts on larger screens.

plurals
  1. POIteration Through Routes Mapped to RouteTable.Routes via MapHttpRoute - MVC4 Web API
    primarykey
    data
    text
    <p>I have mapped a multitude of routes to my MVC4 project as follows:</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // API routing routes.MapHttpRoute( name: "SomeApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // Default controller mapping for non-web api views (testing) routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); //etc. } </code></pre> <p>This works just as planned, and my routes are properly registered. However, later on within my views, I am wanting to only display the api for certain routes, as well as allow the user to select which routes to display the api for. To do this, I need to be able to iterate through the routes that I added and use their string values to filter out the routes I want to use (example below shows how I would set a list to display only api methods using the <code>api/{controller}/{id}</code> route).</p> <pre><code>var list = group.Where(g =&gt; (g.Route.RouteTemplate == "api/{controller}/{id}")); </code></pre> <p>I am unsure exactly how to go about this. I stumbled across <a href="https://stackoverflow.com/questions/6900475/asp-net-mvc-get-urls-to-routetable-routes">this tidbit</a>, but the data within <code>RouteTable.Routes</code> can not be iterated via a <code>Route</code> type, but rather only by a <code>RouteBase</code> type (even though <code>Route</code> extends this class), which has no method that I can see to retrieve the routing string, <code>RouteTemplate</code>.</p> <p>So, it looks like my main conundrum is figuring out how to iterate through <code>RouteTable.Routes</code> and store the HttpRoutes I mapped. Any help/ideas would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload