i started developing application supported web api 2, because wanted retrieve data in restful way.
then needed add html site in order allow administrators manage information provided, created mvc 5 controller return html. want distinguish between web api routes , mvc routes, without affecting, if possible, web api routes, in production.
web api routes: mistake here removing "api" prefix when firstly developed this, have solved collision now
public static void register(httpconfiguration config) { // web api routes config.maphttpattributeroutes(); config.routes.maphttproute( name: "defaultapi", routetemplate: "{controller}/{action}/{id}", defaults: new { controller = "parking", action = "get", id = routeparameter.optional } ); }
mvc routes: default routing. mvc controller name administration, is, mvc routes intended administration site only
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "parking", action = "get", id = urlparameter.optional } ); }
the problem mvc routes don't have opportunity match because webapi routes match first. doesn't matter how mvc routes need changed long work, since not in production yet; it's better me keep web api routes now.
i've tried:
- switching order of registration of web api routes , mvc routes in global.asax.cs
- using "web" prefix mvc controllers, routing mechanism thinks "web" controller , not found (web api routing wins again).
- i couldn't come proper constraint avoid collision.
- hardcode "administration" replacing "{controller}" in mvc route pattern.
is there trick here resolve collision?
the web prefix mvc controllers work. in case, configure routing mvc first (before configuring routing api).
add following constraint api
config.routes.maphttproute( name: "defaultapi", routetemplate: "{controller}/{action}/{id}", defaults: new { controller = "parking", action = "get", id = routeparameter.optional }, constraints: new { controller = @"^web" }, );
Comments
Post a Comment