1. 概述

  ASP.NET MVC route 用来把URL映射到方法中的action,是用户和程序之间的桥梁。

  本章内容包括:定义route处理URL Pattern、应用route限制、忽略URL Pattern、添加route参数、定义areas分组。

2. 主要内容

  2.1 定义route处理URL Pattern

    route解析的灵活性使得URL可以更加的易于理解,又增加了搜索引擎友好性。

    routes保存在一个独立的 RouteCollection对象中。 App_Start/RouteConfig.cs文件中的RegisterRoutes方法用来添加route。

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
}

  2.2 应用route限制

    route限制是一个附加的过滤器,有助于更精确快速的找到请求的资源。

routes.MapRoute(
name: "Videos",
url: "Product/Details/{id}",
defaults: new { controller = "DVD", action = "Index",
Page = UrlParameter.Optional },
constraints: new { id = @"[a-zA-Z]+"}); routes.MapRoute(
name: "Videos2",
url: "Product/Details/{id}",
defaults: new { controller = "DVD", action = "Details",
Page = UrlParameter.Optional },
constraints: new { id = @"\d+" });

  2.3 忽略URL Pattern

    用于忽略的Route,是一种灵活的附加功能,可以提高站点的可预见性和安全性。

routes.Ignore("{*allhtml}", new {allhtml=@".*\.htm(/.*)?});

  2.4 添加route参数

    route参数可以为映射提供更多的灵活性。

routes.MapRoute(
name: "GeneralListDateRange",
url: "{controller} /List/{param1}/{ param2}/{ param3}",
defaults: new { controller = "Article", param1= UrlParameter.Optional,
param2= UrlParameter.Optional, param3= UrlParameter.Optional }, );

  2.5 定义areas

    area用来把一组controllers, views, 和 models 按照一定的功能性组织到一起,多用于大项目中。

    areas间的交互:

@Html.ActionLink("Product Detail", "Product", "Detail",
new { area="ShoppingSite"}, null)

3. 总结

  ① route使得站点url更加简单易读,有利于SEO.

  ② route的添加顺序很重要。先添加ignore route。

  ③ 创建route时,可以在任何可能为空的地方添加默认值。

  ④ Constraints是一种过滤请求Url的方式,可以根据不同route中的变量类型和内容来过滤。

  ⑤ 大型项目中,action的数量可能会巨大。使用areas可以划分逻辑性分组。每一组包含独立的route管理。

第十六章 提升用户体验 之 设计实现routes的更多相关文章

  1. 第十五章 提升用户体验 之 设计实现MVC controllers 和 actions

    1. 概述 controllers 和 actions 是 ASP.NET MVC4中非常重要的组成部分. controller管理用户和程序间的交互,使用action作为完成任务的方式. 如果是包含 ...

  2. Oracle11g温习-第十六章:用户管理

    2013年4月27日 星期六 10:50 1.概念 (1)schema : user.object    就是用户创建的对象 (2)用户认证方式:                            ...

  3. 第十七章 提升用户体验 之 使用MVC扩展功能控制程序行为

    1. 概述 ASP.NET MVC具有很好的扩展性,每一个核心功能都可以被扩展.重写 和 定制. 本章内容包括:实现MVC过滤器和controller工厂.使用 action results,view ...

  4. 高性能MySQL之【第十六章MySQL用户工具】学习记录

    接口工具:      Msql Workbench   http://www.mysql.com/products/workbench      SQLyog  http://www.webyog.c ...

  5. 【linux高级程序设计】(第十六章)网络服务器应用设计

    xinetd服务介绍 xinetd是Linux下的一个网络守候进程,用来统一管理网络负载不大的一组小型网路服务. 一些小型的网络服务,比如时间,telnet服务,不以守候进程出现,而是让xinetd服 ...

  6. Gradle 1.12用户指南翻译——第二十六章. War 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  7. Gradle 1.12用户指南翻译——第三十六章. Sonar Runner 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  8. 读《构建之法》一、二、十六章随笔a

    第一章    概论 “软件团队要从需求分析开始,把合适的需求梳理出来,然后逐步开展后续工作”:——p3 问题:好的用户体验要从软件分析开始,那么软件分析仅仅是从用户的需求出发吗? 我的看法:需求分析是 ...

  9. Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率..

    Atitit.hybrid混合型应用 浏览器插件,控件的实现方式 浏览器运行本地程序的解决方案大的总结---提升用户体验and开发效率.. 1. hybrid App 1 1.1. Hybrid Ap ...

随机推荐

  1. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

    DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...

  2. ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)

    Ignatius and the Princess Description        "Well, it seems the first problem is too easy. I w ...

  3. PageMethods

    主要是用到ScriptManger,老知识Mark一下,ScriptManager要EnablePageMethods="true" 后台 using System.Web.Scr ...

  4. Hive操作笔记

    hive库清表,删除数据 insert overwrite table lorry.bigdata select * from lorry.bigdata where 1=0 hive的simple模 ...

  5. C#支持从自定义日期时间格式到DateTime类型

            /// <summary>         ///         /// </summary>         /// <param name=&quo ...

  6. Spring boot 学习八 Springboot的filter

    一:  传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...

  7. 获取CPU ID ,disk ID, MAC ID (windows ARM linux Mac)

    windows 命令行获取CPU ID,可以用ShellExecute wmic cpu get processorid ProcessorId BFEBFBFF000506E3 开源库: 查询CPU ...

  8. angular.foreach 格式

    angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” 格式: var objs =[{a:1},{a: ...

  9. [Forward]Improving Web App Performance With the Chrome DevTools Timeline and Profiles

    Improving Web App Performance With the Chrome DevTools Timeline and Profiles We all want to create h ...

  10. 远程访问Linux系统桌面

     让Windows可以远程访问Linux系统桌面 http://jingyan.baidu.com/article/d8072ac47b810eec95cefde8.html linux系统下,11款 ...