Reputation: 3238
I'm trying to add custom route to the controller with [RoutePrefix("front")]
that contains three methods:
[HttpGet]
[ResponseType(typeof(SparePartsFrontDTO))]
public async Task<IHttpActionResult> Get()
{
...
}
[HttpGet]
[ResponseType(typeof(IEnumerable<SparePartSearchDTO>))]
public async Task<IHttpActionResult> Get(string filter)
{
...
}
[HttpGet]
[Route("categories")]
[ResponseType(typeof(IEnumerable<DeviceCategoryDTO>))]
public async Task<IHttpActionResult> GetCategories()
{
...
}
But when I'm calling method by the route api/front/categories
, instead of the custom route, default get method was called.
Here is WebApi config:
config.MapHttpAttributeRoutes();
config.MessageHandlers.Add(new PreflightRequestsHandler());
config.SuppressDefaultHostAuthentication();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Upvotes: 2
Views: 667
Reputation: 247018
The route prefix needs to have the desired template plus the other actions need Route
attribute as well.
[RoutePrefix("api/front")]
public class MyController : ApiController {
[HttpGet]
[Route("")] // matches GET api/front
[ResponseType(typeof(SparePartsFrontDTO))]
public async Task<IHttpActionResult> Get() {
//...
}
[HttpGet]
[Route("filter/{filter}")] // matches GET api/front/filter/anything-here
[ResponseType(typeof(IEnumerable<SparePartSearchDTO>))]
public async Task<IHttpActionResult> Get(string filter) {
//...
}
[HttpGet]
[Route("categories")] //matches GET api/front/categories
[ResponseType(typeof(IEnumerable<DeviceCategoryDTO>))]
public async Task<IHttpActionResult> GetCategories() {
//...
}
}
Reference Attribute Routing in ASP.NET Web API 2 : Route Prefixes
Upvotes: 1