RoutePrefix vs Route

I understand that RoutePrefix does not add a route to the routing table by itself. Your actions must have a Route attribute declared. I find it difficult to find a reputable blog / msdn page / something that indicates why defalut RoutePrefix does not add a route to the routing table.

Does anyone have an authoritative post that really contains this, and if so, let me know who it is. Thank you very much.

Edit Refine my question

DOES NOT WORK

 [RoutePrefix("api/Steve")] public class SteveController : ApiController { public int get(){return 1000000;} } 

Works

 [RoutePrefix("api/Steve")] public class SteveController : ApiController { [Route("")] public int get(){return 1000000;} } 

The above script works because we explicitly stated that the get action on the SteveController has an empty route. Once we do this, the route is added to the RouteTable

The first scenario does not work, because only using RoutePrefix does not add anything to the route table. RoutePrefix alone will not generate a route. It seems to be well known, I want to know the source, which says why this is so. The preferred community member I trust is either John Skeet or someone from the Microsoft team.

+5
source share
2 answers

The route prefix is โ€‹โ€‹associated with design routes in attribute routing.

Used to set a common prefix for the entire controller.

If you read the release notes that introduced this feature, you can get a better idea of โ€‹โ€‹the subject.

ASP.NET Web API 2

Attribute Routing

ASP.NET Web API now supports attribute routing, thanks to Tim McCall's input. When routing attributes, you can specify web API routes to annotate your actions and controllers as follows:

 [RoutePrefix("orders")] public class OrdersController : ApiController { [Route("{id}")] public Order Get(int id) { } [Route("{id}/approve")] public Order Approve(int id) { } } 

Attribute routing gives you more control over the URIs in your API network. For example, you can easily define a hierarchy of resources using a single API controller:

 public class MoviesController : ApiController { [Route("movies")] public IEnumerable<Movie> Get() { } [Route("actors/{actorId}/movies")] public IEnumerable<Movie> GetByActor(int actorId) { } [Route("directors/{directorId}/movies")] public IEnumerable<Movie> GetByDirector(int directorId) { } } 

What's New in ASP.NET Web API 2.1

What's New in ASP.NET Web API 2.2

Really nice related article

ASP.NET 5 Deep Dive: Routing

While there is no expert on this, here is my understanding of how this works.

When routing attributes, the infrastructure checks the route attribute for controller actions to create route entries to add to the route table. Therefore, while you are using attribute routing, you will use [RouteAttribute] . Without this attribute, the action will by default revert to convention-based routing. RoutePrefixAttribute is an extensibility point that allows you more control over how you define your routes / Urls. Release notes say the same.

Besides my understanding and the last link, everything else was cited from MS documentation.

+7
source

For an authoritative source, here are descriptions from MSDN (emphasis mine).

RouteAttribute

Place on the controller or action to open it directly through the route . When it is placed on the controller, it applies to actions that System.Web.Mvc.RouteAttributes do not have on them.

RoutePrefixAttribute

Annotates the controller with a route prefix that applies to all actions in the controller.

As you can see, the description for Route mentions an action action, but RoutePrefix does not.

0
source

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


All Articles