属性路由ASP.NET Web API 现在支持属性路由,这要归功于蒂姆·麦考尔的贡献.使用属性路由,您可以通过以下方式指定 Web API 路由像这样注释你的动作和控制器:[RoutePrefix("orders")]公共类 OrdersController : ApiController{[路线({id}")]公共订单获取(int id){}[路线({id}/批准")]公共订单批准(int id){}}属性路由使您可以更好地控制 Web 中的 URI应用程序接口.例如,您可以使用单个 API 控制器:public class MoviesController : ApiController{[路线(电影")]公共 IEnumerable得到() { }[路线(演员/{actorId}/电影")]公共 IEnumerableGetByActor(int actorId) { }[Route("directors/{directorId}/movies")]公共 IEnumerableGetByDirector(int directorId) { }}ASP 的新功能.NET Web API 2.1最新消息在 ASP.NET Web API 2.2 中关于这个主题的一篇非常好的文章ASP.NET 5 深入探讨:路由虽然没有这方面的专家,但这是我对其工作原理的理解.通过属性路由,框架会检查控制器操作上的路由属性,以便创建路由条目以添加到路由表中.因此,只要您使用属性路由,您就将使用 [RouteAttribute].如果没有此属性,操作将默认返回到基于约定的路由.RoutePrefixAttribute 是一个扩展点,允许您更好地控制定义路由/Urls 的方式.发行说明也说了这么多.除了我的理解和提供的最后一个链接外,其他所有内容均引用自 MS 文档.I understand that RoutePrefix doesn't add a route to the routing table by itself. On your actions you need to have a Route attribute declared. I am having a hard time finding an authoritative blog/msdn page/ something that states why by defalut RoutePrefix doesn't add a route to the routing table.Does anyone have an authoritative post that does contain this to be the case, and if so will you let me know whom it is. Thank you very much.EditTo Clarify my questionDOESN'T 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 scenario works because we explicitly stated that the get action on the SteveController has an empty route. Once we do that the route is added to the RouteTableThe first scenario doesn't work, because just using RoutePrefix doesn't add anything to the route table. RoutePrefix by itself will not generate a route.This seems to be common knowledge, I want to find a trusted source, like official Microsoft documentation, that states why this is. 解决方案 Route prefixes are associated with routes by design in attribute routing.It is used to set a common prefix for an entire controller.If you read the release notes that introduced the feature you may get a better understanding of the subject.ASP.NET Web API 2 Attribute routing ASP.NET Web API now supports attribute routing, thanks to a contribution by Tim McCall. With attribute routing you can specify your Web API routes by annotating your actions and controllers like this:[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 web API. For example, you can easily define a resource hierarchy 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.1What's New in ASP.NET Web API 2.2A really good article on the subjectASP.NET 5 Deep Dive: RoutingWhile no expert on the subject, here is my understanding of how this works.With attribute routing the framework inspects the route attribute on the actions of a controller in order to create route entries to add to the route table. So as long as you are using attribute routing you are going to be using the [RouteAttribute]. Without this attribute the action will default back to convention-based routing. The RoutePrefixAttribute is an extensibility point that allows you more control of how you define your routes/Urls. The release notes say as much.Other than my understanding and the last link provided, everything else was quoted from MS documentation. 这篇关于RoutePrefix 与路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 22:24
查看更多