As I've created a seperate search controller with multiple get methods, I needed to modify the default routes.
SearchController.cs
[HttpGet]
public IEnumerable<Object> CompanyByOrderId(long id) { ... }
[HttpGet]
public IEnumerable<Object> CompanyByName(string name) { ... }
[HttpGet]
public IEnumerable<Object> UserByName(string name) { ... }
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ActionById",
routeTemplate: "api/Search/{action}/{id}",
defaults: new { controller = "Search", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ActionByName",
routeTemplate: "api/Search/{action}/{name}",
defaults: new { controller = "Search", name = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Referring to http://localhost:52498/api/Search/CompanyByOrderId/1 works fine. But calling one of the other methods with a name as parameter results in 404 error. E.g. http://localhost:52498/api/Search/UserByName/john
How do I specify that the URI with the string parameter is mapped to the CompanyByName / UserByName methods?