In my WebApiConfig.cs file I have :
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
I have a OrderController
[RoutePrefix("api/Orders")]
public class OrderController : ApiController
{
[Authorize]
[Route("")]
public IHttpActionResult Get()
{
return Ok(Order.CreateOrders());
}
As expected, the above code works with url - http://localhost:15660/api/Orders
I got another CustomerController :
[Authorize]
[RoutePrefix("api/Customers")]
public class CustomerController : ApiController
{
// GET api/customers/search
[HttpGet]
[Route("search/{location}/{customerName}/{phoneNumber}/{email}")]
public IHttpActionResult SearchCustomers(string location = null, string customerName = null, string phoneNumber = null, string email = null)
{
return Ok(GetCustomersSearchResults(location, customerName, phoneNumber, email));
}
Here, I want to call as /api/Customers/search - but this gives error for no match controller name found. If I rename the prefix to
[RoutePrefix("api/Customer")]
then it works perfectly well.
In Ordercontroller, api/Orders
works perfectly well. In CustomerController, why api/customers
doesn't work at all and gives error. I googled a lot, found the syntax is correct, but can't figure where am I going wrong that is restricting CustomerController to map with /api/Customers/search
Can anyone please help me know how to map CustomerController the way I want to using [RoutePrefix].
Thanks a lot.