RoutePrefix 与路由 [英] RoutePrefix vs Route

查看:24
本文介绍了RoutePrefix 与路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 RoutePrefix 本身不会向路由表添加路由.在您的操作中,您需要声明一个 Route 属性.我很难找到一个权威的博客/msdn 页面/一些说明为什么默认 RoutePrefix 不向路由表添加路由的内容.

有没有人有一个权威的帖子确实包含这种情况,如果有,你让我知道是谁.非常感谢.

编辑澄清我的问题

不起作用

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

作品

[RoutePrefix(api/Steve")]公共类 SteveController : ApiController{[路线(")]public int get(){return 1000000;}}

上述场景有效是因为我们明确声明 SteveController 上的 get 操作有一个空路由.一旦我们这样做,路由就会被添加到 RouteTable

第一种情况不起作用,因为仅使用 RoutePrefix 不会向路由表添加任何内容.RoutePrefix 本身不会生成路由.这似乎是常识,我想找到一个可信的来源,比如微软官方文档,说明为什么会这样.

解决方案

路由前缀在属性路由中通过设计与路由相关联.

用于为整个控制器设置通用前缀.

如果您阅读介绍该功能的发行说明,您可能会对该主题有更好的理解.

ASP.NET Web API 2<块引用>

属性路由

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}/电影")]公共 IEnumerable<电影>GetByActor(int actorId) { }[Route("directors/{directorId}/movies")]公共 IEnumerable<电影>GetByDirector(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.

Edit To Clarify my question

DOESN'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 RouteTable

The 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.1

What's New in ASP.NET Web API 2.2

A really good article on the subject

ASP.NET 5 Deep Dive: Routing

While 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 与路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆