RoutePrefix与Route [英] RoutePrefix vs Route

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

问题描述

我知道RoutePrefix本身不会向路由表添加路由.在执行操作时,需要声明一个Route属性.我很难找到权威的blog/msdn页面/,该内容说明了为什么defalut RoutePrefix不会将路由添加到路由表中.

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

不工作

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

作品

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

上述情况之所以有效,是因为我们明确声明了SteveController上的get操作具有空路由.一旦完成,该路由即被添加到RouteTable

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

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

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

属性路由

ASP.NET Web API现在支持属性路由,这要归功于 蒂姆·麦考尔(Tim McCall)的贡献. 使用属性路由,您可以通过以下方式指定Web API路由 像这样注释您的动作和控制器:

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) { } 
} 

属性路由使您可以更好地控制Web中的URI API.例如,您可以使用 单个API控制器:

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) { } 
} 

ASP的新增功能.NET Web API 2.1

新功能在ASP.NET Web API 2.2中

关于该主题的一篇非常好的文章

A really good article on the subject

ASP.NET 5深潜:路由

虽然没有这个主题的专家,但是我对这是如何工作的理解.

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

使用属性路由,框架会检查控制器动作的路由属性,以创建要添加到路由表的路由条目.因此,只要您使用属性路由,就将使用[RouteAttribute].如果没有此属性,则该操作将默认返回基于约定的路由. RoutePrefixAttribute是可扩展性点,它使您可以更好地控制如何定义路径/URL.发行说明也是如此.

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.

除了我的理解和所提供的最后一个链接之外,MS文档中还引用了其他所有内容.

Other than my understanding and the last link provided, everything else was quoted from MS documentation.

这篇关于RoutePrefix与Route的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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