(using Web api 1.0) I have a scenario where my action has multiple parameters like GetCustomer(int id, string email) where the url would be specified as
GET api/Customer/{id}/{email}
But i am looking to configured the url as
api/Customer/{id}?email={email}
So it's a combination if url segmentation with query string. Currently when i am trying to set this i am getting this below error.
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character. Parameter name: routeUrl
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Help Area",
"",
new { controller = "Help", action = "Index" }
).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
kindly help me in fixing this thanks in advance
Your configuration needs updating
public static class WebApiConfig
{
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "CustomerApi_GetCustomer",
routeTemplate: "api/Customer/{id}",
defaults: new { controller = "Customer" action = "GetCustomer" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
If you make a request to api/Customer/{id}?email={email}
the binder will automatically match the GetCustomer(int id, string email)
with the email
parameter from the url