属性路由(attribute routing)是最新被引进到MVC5中的,它和传统路由共同组成了mvc5中的两种主要路由.

1.  高质量的url应该满足以下几点

  1. 域名便于记忆和拼写
  2. 简短
  3. 便于输入
  4. 可以反映出站点结构
  5. 应该是“可破解的”,用户可以通过移除url的末尾,进而到达更高层次的信息体系结构(URLs that are hackable to allow users to move to higher levels of the information architecture by hacking off the end of the URL)
  6. 持久不能改变

在MVC5中URL(Uniform Resource Locator)统一资源定位器 中的Resource代表抽象的概念,可以代表一个文件,也可以代表方法调用返回的结果,或者其他一些资源。

URI 代表 Uniform Resource Identifier,即同一资源标识符,

2.  MVC中包含有两种路由:属性路由和传统路由

路由是你应用程序的入口点(entry points)。

A route definition starts with a URL template, which specifies the pattern that the route will match.

路由以其匹配的url模板开始

组合使用属性路由和传统路由

在实际应用场景中,通常在RegisterRoutes方法中将属性路由置于传统路由之前,因为属性路由和传统路由重叠的话,mvc会优先选择先注册的路由。属性路由一般应用于专用场景而传统路由更偏向于通用场景。

比如你有一个使用传统路由的应用程序,现在你想增加一个新的控制器去使用属性路由,这将是很容易办到的。

首先启用属性路由

        public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes(); //启用属性路由
routes.MapRoute("simple",
"{controller}/{action}/{id}",
new {action = "index", id = UrlParameter.Optional}); }

然后,应用属性路由到具体的控制器中

// Existing class
public class HomeController : Controller
{
public ActionResult Index()
{
  return View();
}
public ActionResult About()
{
  return View();
}
public ActionResult Contact()
{
  return View();
}
}
[RoutePrefix("contacts")]
[Route("{action=Index}/{id?}")]
public class NewContactsController : Controller
{
public ActionResult Index()
{
  // Do some work
  return View();
}
public ActionResult Details(int id)
{
  // Do some work
  return View();
}
public ActionResult Update(int id)
{
  // Do some work
  return View();
}
public ActionResult Delete(int id)
{
  // Delete the contact with this id
  return View();
}
}

3.  具体选用属性路由还是传统路由呢?

    以下场景建议选择属性路由

      a.  你希望你的路由和你的action在一起

      b.  你重新创建一个新的应用程序,或者你对应用程序做重大修改时

    以下场景建议选择传统路由

      a.  你想集中配置你所有的路由

      b.  你使用自定义约束对象

      c.  你不想改变已经存在的应用程序

下面是建议使用属性路由的英文描述:

The centralized configuration of traditional routes means there’s one place to go to understand how
a request maps to an action. Traditional routes also have some more flexibility than attribute routes.
For example, adding a custom constraint object to a traditional route is easy. Attributes in C# only
support certain kinds of arguments, and for attribute routing, that means everything is specified in
the route template string.
On the other hand, attribute routing nicely keeps everything about your controllers together, including
both the URLs they use and the actions that run. I tend to prefer attribute routing for that
reason. Fortunately, you can use both and moving a route from one style to the other if you change
your mind is not difficult.

MVC5 属性路由的更多相关文章

  1. 【翻译】ASP.NET MVC 5属性路由(转)

    转载链接:http://www.cnblogs.com/thestartdream/p/4246533.html 原文链接:http://blogs.msdn.com/b/webdev/archive ...

  2. WebApi 2:属性路由 [Route()],attribute routing

    原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 属性 ...

  3. Asp.Net Web API 2第八课——Web API 2中的属性路由

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 路由就是Web API如何 ...

  4. [水煮 ASP.NET Web API2 方法论](3-2)直接式路由/属性路由

    问题 怎么样可以使用更贴近资源(Controller,Action)的方式定义路由. 解决方案 可以使用属性路由直接在资源级别声明路由.只要简单的在 Action 上使用属性路由 RouteAttri ...

  5. ASP.NET Web API 2 中的属性路由使用(转载)

    转载地址:ASP.NET Web API 2 中的属性路由使用

  6. WebApi(三)-属性路由 自定义访问路径

    启用属性路由: 1.在WebApiConfig中加入如下代码: //属性路由 config.MapHttpAttributeRoutes();

  7. Web API 2中的属性路由

    Web API 2中的属性路由 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.ht ...

  8. MVC 5 属性路由中添加自己的自定义约束

    介绍约束 ASP.NET MVC和web api 同时支持简单和自定义约束,简单的约束看起来像: routes.MapRoute("blog", "{year}/{mon ...

  9. Web API中的路由(二)——属性路由

    一.属性路由的概念 路由让webapi将一个uri匹配到对应的action,Web API 2支持一种新类型的路由:属性路由.顾名思义,属性路由使用属性来定义路由.通过属性路由,我们可以更好地控制We ...

随机推荐

  1. C#利用GDI+绘制旋转文字等效果

    C#中利用GDI+绘制旋转文本的文字,网上有很多资料,基本都使用矩阵旋转的方式实现.但基本都只提及按点旋转,若要实现在矩形范围内旋转文本,资料较少.经过琢磨,可以将矩形内旋转转化为按点旋转,不过需要经 ...

  2. mongodb的分布式集群(4、分片和副本集的结合)

    概述 前面3篇博客讲了mongodb的分布式和集群,当中第一种的主从复制我们差点儿不用,没有什么意义,剩下的两种,我们不论单独的使用哪一个.都会出现对应的问题.比較好的一种解决方式就是.分片和副本集的 ...

  3. javascript complete, onload

    1.complete 属性可返回浏览器是否已完成对图像的加载 <html> <head> <script type="text/javascript" ...

  4. RHEL 7 命令行注册和激活订阅服务

    导读 前一阵子,红帽推出了开发者免费使用订阅功能,只要注册成为红帽开发者就可以免费使用包括  RHEL7  在内的开发套件. 今天我们就来看一看怎么使用命令行来快速注册和激活订阅服务,以后就可以方便地 ...

  5. 乐观锁(optimistic locking)与悲观锁(pessimistic locking)

    首先,乐观锁(optimistic locking)与悲观锁(pessimistic locking)基本是针对数据处理来说,也就是跟数据库有关的术语,目的是为了解决并发处理时所遇到的相关性能问题,以 ...

  6. Java再学习——停止一个正在运行的线程

    关于这个问题,先了解一下Thread类方法中被废弃的那些方法.suspend(), resume(),stop()/stop(Throwable obj),destroy() 首先,stop(Thro ...

  7. Apple Tree(需要预处理的树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20335   Accepted: 6182 Descr ...

  8. spring mvc 的各种参数的绑定方式

    本文转自http://www.cnblogs.com/HD/p/4107674.html SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: ...

  9. 基于http协议的api接口对于客户端的身份认证方式以及安全措施

    由于http是无状态的,所以正常情况下在浏览器浏览网页,服务器都是通过访问者的cookie(cookie中存储的jsessionid)来辨别客户端的身份的,当客户端进行登录服务器也会将登录信息存放在服 ...

  10. 【Shell脚本学习10】Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等

    Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最 ...