How can I integrate regex into my MapHttpRoute for WebApi

Viewed 1316

I currently have:

config.Routes.MapHttpRoute(
                name: "AccountApiCtrl",
                routeTemplate: "/api" + "/account/{action}/{id}",
                defaults: new { controller = "accountapi", id = RouteParameter.Optional }
            );

Which sends /api/account/myaction requests to accountapi controller. (it works great :-))

However, I'd rather not do this for each controller I have, is there a way I can use regex here so that I only have on declaration?

I.E so:

/api/account/action goes to controller: AccountApi and /api/user/action goes to controller: UserApi

i.e something along the lines of:

config.Routes.MapHttpRoute(
                name: "ControllerApiMap",
                routeTemplate: "/api" + "/{controller}/{action}/{id}",
                defaults: new { controller = "{controller}+api", id = RouteParameter.Optional }
            );
2 Answers
Related