This allows routing with Attribute on any action. This works for both Api as well as mvc controller. You can have multiple route to an action. This can also be applied at the controller level
[Route("Api/Product/{name:alpha}/Reviews")]
[Route("Api/Product/{id:int:min(1)}/Reviews")]
public HttpResponseMessage GetReviews(int id)
[RoutePrefix("Api/Product")] : This will apply to all the routes in the controller
Following configuration change need to happen to make attribute route work
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{name}", new {name = RouteParameter.Optional});
Notice MapHttpAttributeRoutes is before default route, this give opportunity for attribute route to be applied before default route.
No comments:
Post a Comment