MVC5 属性路由
属性路由(attribute routing)是最新被引进到MVC5中的,它和传统路由共同组成了mvc5中的两种主要路由.
1. 高质量的url应该满足以下几点
- 域名便于记忆和拼写
- 简短
- 便于输入
- 可以反映出站点结构
- 应该是“可破解的”,用户可以通过移除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)
- 持久不能改变
在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 属性路由的更多相关文章
- 【翻译】ASP.NET MVC 5属性路由(转)
转载链接:http://www.cnblogs.com/thestartdream/p/4246533.html 原文链接:http://blogs.msdn.com/b/webdev/archive ...
- WebApi 2:属性路由 [Route()],attribute routing
原文:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 属性 ...
- Asp.Net Web API 2第八课——Web API 2中的属性路由
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 路由就是Web API如何 ...
- [水煮 ASP.NET Web API2 方法论](3-2)直接式路由/属性路由
问题 怎么样可以使用更贴近资源(Controller,Action)的方式定义路由. 解决方案 可以使用属性路由直接在资源级别声明路由.只要简单的在 Action 上使用属性路由 RouteAttri ...
- ASP.NET Web API 2 中的属性路由使用(转载)
转载地址:ASP.NET Web API 2 中的属性路由使用
- WebApi(三)-属性路由 自定义访问路径
启用属性路由: 1.在WebApiConfig中加入如下代码: //属性路由 config.MapHttpAttributeRoutes();
- Web API 2中的属性路由
Web API 2中的属性路由 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.ht ...
- MVC 5 属性路由中添加自己的自定义约束
介绍约束 ASP.NET MVC和web api 同时支持简单和自定义约束,简单的约束看起来像: routes.MapRoute("blog", "{year}/{mon ...
- Web API中的路由(二)——属性路由
一.属性路由的概念 路由让webapi将一个uri匹配到对应的action,Web API 2支持一种新类型的路由:属性路由.顾名思义,属性路由使用属性来定义路由.通过属性路由,我们可以更好地控制We ...
随机推荐
- 【开发实例】C#调用SAPI实现语音合成的两种方法
我们都知道现在的语音合成TTS是可以通过微软的SAPI实现的,好处我就不多说了,方便而已,因为在微软的操作系统里面就自带了这个玩意,主要的方式有两种: 1.使用COM组件技术,不管是C++,C#,D ...
- tcpdump抓包分析具体解释
說實在的,對於 tcpdump 這個軟體來說,你甚至能够說這個軟體其實就是個駭客軟體, 因為他不但能够分析封包的流向,連封包的內容也能够進行『監聽』, 假设你使用的傳輸資料是明碼的話,不得了,在 ro ...
- Android 开发之异常处理篇(一):SDK Manager 闪退的解决方法
这个问题困扰了我很久,之前没解决,就放一放.后来我又专门拿了一个下午来找解决方法,终于搞定! 我的解决方法是修改 android.bat,直接指定java.exe所在位置,不用去调用find_java ...
- 【JavaScript】你知道吗?Web的26项基本概念和技术
Web开发是比较费神的,需要掌握很多很多的东西,特别是从事前端开发的朋友,需要通十行才行.今天,本文向初学者介绍一些Web开发中的基本概念和用到的技术,从A到Z总共26项,每项对应一个概念或者技术. ...
- TP复习11
## ThinkPHP 3.1.2 模板中的基本语法#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.导入CSS和JS文件 1.css link js ...
- [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route
In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...
- HTML <base> 标签
定义和用法 <base> 标签为页面上的所有链接规定默认地址或默认目标. 通常情况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白. 使用 <base& ...
- Swift2.0 中的String(三):类型转换
本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...
- 实现顶部轮播,下部listview经典布局的两种方式
开头: 在做android开发的时候,我们经常会遇到这样的布局,上面是一个图片轮播图,下面是一些列表的项目.很多新闻app,视频类app都采用这样的布局.起初的时候 由于没有很多参考,我自己想到了一种 ...
- Cocos2d-x v3.0 新的事件调度方法 lambda表达式的使用
欢迎添� Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/24603251 Coc ...