It sounds like you need to add new actions to your controller. You will need to modify (or add to) your Routes in your WebApiConfig.cs file to include action mapping.
For example, let's say you update your Controller with the additional functions GetTestString1 and GetTestString2:
public class TestController : ApiController
{
public String GetTestString1(int id)
{
return "Test String 1 for " + id;
}
public String GetTestString2(int id)
{
return "Test String 2 for " + id;
}
}
To perform the routing to these new functions you need to add the following to the WebApiConfig.cs file in the App_Start folder:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// This mapping entry may already exist for you. Leave it alone
// so your existing default functions continue to work properly.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Add this to route your new functions:
config.Routes.MapHttpRoute(
name: "TestStringApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
The above routeTemplate "api/{controller}/{action}/{id}" is how the request URL will be routed to your controller and actions (functions). The {controller} part of the URL will route to your TestController class. The {action} part of the URL is the additional function you asked about and will be mapped to GetTestString1 or GetTestString2 depending on what you request in your URL.
When you open your browser to this address,
http://localhost:60303/api/Test/GetTestString1/100
the route you registered in the WebApiConfig.cs will map the url to your Testcontroller's GetTestString1 action (function) with 100 as the input parameter and will return "Test String 1 for 100" to the browser.
You can call your Testcontroller's GetTestString2 action (function) like this
http://localhost:60303/api/Test/GetTestString2/101
and "Test String 2 for 101" will be returned to the browser.
You can learn more about the how the default functions work (get, post delete) and more about actions and parameters here:
Here are some related discussions similar to this topic: