The [Route] attribute does not work without [RoutePrefix] in ASP.NET WebApi

Refer to the official ASP.NET attribute attribution documentation , it seems that the Route attribute can be used without RoutePrefix .

But in my webapi controller below there are cases.

1. Does not work. (error: no suitable http route was found)

 public class GroupController : ApiController { [Route("api/group/{id}/register")] public IHttpActionResult Post(int id, InputModel model) { return Ok(); } } 

2. Works well.

 [RoutePrefix("api/group")] public class GroupController : ApiController { [Route("{id}/register")] public IHttpActionResult Post(int id, InputModel model) { return Ok(); } } 

Is it correct that the Route attribute should be used with RoutePrefix , or what am I missing?

Also, the code below is my WebApi route configuration in the WebApiConfig.Register class.

  // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}/{subId}", defaults: new { id = RouteParameter.Optional, subId = RouteParameter.Optional } ); 
+5
source share

Source: https://habr.com/ru/post/1201170/


All Articles