After reading lots of answers finally I figured out.
First, I added 3 different routes into WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null,
constraints: new { name = @"^[a-z]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" }
);
}
Then, removed ActionName, Route, etc.. from the controller functions. So basically this is my controller;
// GET: api/Countries/5
[ResponseType(typeof(Countries))]
//[ActionName("CountryById")]
public async Task<IHttpActionResult> GetCountries(int id)
{
Countries countries = await db.Countries.FindAsync(id);
if (countries == null)
{
return NotFound();
}
return Ok(countries);
}
// GET: api/Countries/tur
//[ResponseType(typeof(Countries))]
////[Route("api/CountriesByName/{anyString}")]
////[ActionName("CountriesByName")]
//[HttpGet]
[ResponseType(typeof(Countries))]
//[ActionName("CountryByName")]
public async Task<IHttpActionResult> GetCountriesByName(string name)
{
var countries = await db.Countries
.Where(s=>s.Country.ToString().StartsWith(name))
.ToListAsync();
if (countries == null)
{
return NotFound();
}
return Ok(countries);
}
Now I am able to run with following url samples(with name and with id);
http://localhost:49787/api/Countries/GetCountriesByName/France