MVC5 路由

属性路由(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.
时间: 2024-08-25 00:04:54

MVC5 路由的相关文章

MVC5路由系统机制详细讲解

请求一个ASP.NET mvc的网站和以前的web form是有区别的,ASP.NET MVC框架内部给我们提供了路由机制,当IIS接受到一个请求时,会先看是否请求了一个静态资源(.html,css,js,图片等),这一步是web form和mvc都是一样的,如果不是说明则说明是请求的一个动态页面,就会走asp.net的管道,mvc的程序请求都会走路由系统,会映射到一个Controller对应的Action方法,而web form请求动态页面是会查找本地实际存在一个aspx文件.下面通过一个AS

ASP.NET MVC5路由系统机制详细讲解

请求一个ASP.NET mvc的网站和以前的web form是有区别的,ASP.NET MVC框架内部给我们提供了路由机制,当IIS接受到一个请求时,会先看是否请求了一个静态资源(.html,css,js,图片等),这一步是web form和mvc都是一样的,如果不是则说明是请求的是一个动态页面,就会走asp.net的管道,mvc的程序请求都会走路由系统,会映射到一个Controller对应的Action方法,而web form请求动态页面是会查找本地实际存在一个aspx文件.下面通过一个ASP

ASP.NET MVC5+ 路由特性

概述 ASP.NET MVC 5支持一种新的路由协议,称为路由特性. MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 案例 1.使用Visual Studio 2013 创建一个 4.5.1的 Web 项目,选择MVC框架 2.配置RouteConfig.cs 附图: 3.现在就可以在控制器中使用路由属性了 附图 4.约束图 本图摘自 园友 Wolfy 文章 运行结果 总结 ASP.NET MVC5 正在学习中,欢迎一起交流学习. .NET技术交流群 1992

Asp.net MVC]Asp.net MVC5系列——Routing特性

目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列--第一个项目 [Asp.net MVC]Asp.net MVC5系列--添加视图 [Asp.net MVC]Asp.net MVC5系列--添加模型 [Asp.net MVC]Asp.net MVC5系列--从控制器访问模型中的数据 [Asp.net MVC]Asp.net MVC5系列--添加数据

MVC框架三大模块

1.Routing模块 Routing机制与MVC5的URL解析处理流程基本是相同的,很多的接口和类像IRouteHandler.IHttpHandler.IController .RouteBase.RouteTable.RouteDictionary.DefaultControllerFactory以及AreaRegistration名称是相同的功能是类似的,也可以是看做MVC5路由机制的简化版本或者是Artech MVC5书中路由实例的强化版本.在Web项目的启动阶段,设置的路由模板(包括

ASP.NET MVC5 新特性:Attribute路由使用详解

1.什么是Attribute路由?怎么样启用Attribute路由? 微软在 ASP.NET MVC5 中引入了一种新型路由:Attribute路由,顾名思义,Attribute路由是通过Attribute来定义路由.当然,MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 在以前的版本中我们通常在 RouteConfig.cs 文件中通过以下方式来定义路由: routes.MapRoute( name: "ProductPage", url: &quo

MVC5中路由新特性

1.什么是Attribute路由?怎么样启用Attribute路由? 微软在 ASP.NET MVC5 中引入了一种新型路由:Attribute路由,顾名思义,Attribute路由是通过Attribute来定义路由.当然,MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 在以前的版本中我们通常在 RouteConfig.cs 文件中通过以下方式来定义路由: routes.MapRoute( name: "ProductPage", url: &quo

ASP.NET MVC5 新特性:Attribute路由使用详解 (转载)

1.什么是Attribute路由?怎么样启用Attribute路由? 微软在 ASP.NET MVC5 中引入了一种新型路由:Attribute路由,顾名思义,Attribute路由是通过Attribute来定义路由.当然,MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 在以前的版本中我们通常在 RouteConfig.cs 文件中通过以下方式来定义路由: routes.MapRoute( name: "ProductPage", url: &quo

ASP.NET MVC5(一)—— URL路由

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Mvc.Routing.Constraints; 7 using System.Web.Routing; 8 9 namespace UrlAndRoutes 10 { 11 public class RouteConfig 1