Wednesday 15 May 2013

c# - Using ASP.NET WebApi, how can I make a route exclude a set of controllers? -



c# - Using ASP.NET WebApi, how can I make a route exclude a set of controllers? -

i have created 2 controllers exporting type schemas , wadl , else part of api

so created 3 routes, 2 special controllers (schema , wadl) , 1 "everything else":

config.routes.maphttproute( name: "schemas", routetemplate: "api/{controller}/{typename}/{subtype}", defaults: new { subtype = routeparameter.optional }, constraints: new { controller = @"schemas" } ); config.routes.maphttproute( name: "wadl", routetemplate: "api/{controller}", defaults: null, constraints: new { controller = @"wadl" } ); config.routes.maphttproute( name: "api", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional }, constraints: somethingsomethingsomething } );

i tried next page: http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints

and created variation on notequals class:

public class notequal : irouteconstraint { private ienumerable<string> matches; public notequal(ienumerable<string> matches) { matches = matches; } public bool match(httpcontextbase httpcontext, route route, string parametername, routevaluedictionary values, routedirection routedirection) { bool output = true; var name = values[parametername].tostring(); foreach (var match in matches) { if (name.contains(match)) { output = false; break; } } homecoming output; } }

and changed 3rd route be:

config.routes.maphttproute( name: "api", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional }, constraints: new { controller = new notequal(new list<string> { "schemas", "wadl" }) } ); public class notequal : irouteconstraint { private ienumerable<string> matches; public notequal(ienumerable<string> matches) { matches = matches; } public bool match(httpcontextbase httpcontext, route route, string parametername, routevaluedictionary values, routedirection routedirection) { bool output = true; var name = values[parametername].tostring(); foreach (var match in matches) { if (name.contains(match)) { output = false; break; } } homecoming output; } }

doesn't work, still matches 3 controllers

it'll work fine 1 route, auto help page ugly , misleading.

if set break on match, doesn't nail break point.

the regex constraints work perfectly, "everything else" 1 not working

why isn't working?

honestly if can, should utilize attributerouting much clearer in code , much easier new project see route hitting what.

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

c# asp.net regex asp.net-mvc asp.net-web-api

No comments:

Post a Comment