The solution depends on whether the parameter name is important. By default within Microsoft Web Api, the query string parameter name must match the parameter variable name of the method. For example:
If the url is
"api/MenuData/GetMenu?UserPKId=1"
then the controller method must have the following parameter list
public MyModel CommonWebApiMethod(string MethodName, string UserPKId)
Unimportant parameter name
Configure the route:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "MethodName",
routeTemplate: "api/MenuData/{MethodName}",
defaults: new { controller = "Common", action = "CommonWebApiMethod" }
);
}
}
Controller:
public class CommonController : ApiController
{
[HttpPost]
public MyModel CommonWebApiMethod(string MethodName, string parameter)
{
return new MyModel { MethodName = MethodName, Parameter = parameter };
}
}
Calling url:
"api/MenuData/GetMenu?parameter=1"
Important parameter name
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ParameterName",
routeTemplate: "api/MenuData/{MethodName}/{parameterName}",
defaults: new { controller = "Common", action = "CommonWebApiMethod" }
);
}
}
Controller:
public class CommonController : ApiController
{
[HttpPost]
public MyModel CommonWebApiMethod(string MethodName, string parameterName, string parameter)
{
return new MyModel { MethodName = MethodName, Parameter = parameter };
}
}
Calling url:
"api/MenuData/GetMenu/UserPKId?parameter=1"