惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - 十三

ASP.NET MVC5学习笔记之Action参数模型绑定之模型元数据和元数据提供 ASP.NET MVC5学习笔记之Action参数模型绑定值提供体系 ASP.NET MVC5学习笔记之Action参数模型绑定基本过程 ASP.NET MVC5学习笔记之Filter提供体系 ASP.NET MVC5学习笔记之Filter基本介绍 ASP.NET MVC5学习笔记之Controller执行ControllerDescriptor和ActionDescriptor ASP.NET MVC5学习笔记之Controller同步执行架构分析 ASP.NET MVC4学习笔记之Controller激活的扩展 ASP.NET MVC4学习笔记之Controller的激活 ASP.NET MVC4学习笔记路由系统实现 ASP.NET MVC4学习笔记之总体概述 Web Capacity Analysis Tool 压力测试工具使用笔记 WCF完全解析读书笔记第2章地址 CLR via C# 混合线程同步构造 CLR via C# I/O基元线程同步构造 CLR via C# 计算限制的异步操作读书笔记 CLR via C# 线程基础知识读书笔记 CLR via C# 序列化读书笔记 CLR via C# 内存管理读书记
ASP.NET MVC4学习笔记路由系统概念与应用篇
十三 · 2014-03-10 · via 博客园 - 十三

一.概念

  1.路由是计算机网络中的一个技术概念,表示把数据包从一个网段转发至另一网段。ASP.NET中的路由系统作用类似,其作用是把请求Url映射到相应的"资源"上,资源可以是一段代码或具体的Web页面.路由系统提供了一种简单抽象机制,让我们利用Url更加精简方式表示资源,而无关资源具体的实现或表示。 

  2. 路由系统包含一系列重要的概念,现介绍如下:

路由规则(RouteBase): 表示其中具体的一条路由条件,如果满足该路由条件,则转发至该路由处理. 路由规则由路由名称,路由默认值,Url路由模式,约束,附加数据这几部分组成

路由表(RouteCollection): 路由规则的集合

路由数据(RouteData): 表示路由匹配的结果,包含当前路由的数据 

路由处理(IRouteHandler): 表示路由处理,返回一个IHttpHandler处理当前请求

  3.路由与Url重写的区别

    Url重写从设计目的来说是把一个Url映射到另一个Url, 功能比较单一,路由是把Url映射一个系统资源,另外,路由系统也支持生成Url, 通俗说支持双向"路由".

二.应用

  1. 为请求Url 检查路由匹配

    新建一个ASP.NET MVC4 Internet项目,在App_Start目录RouteConfig.cs 有如下代码:

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

   routes.IgnoreRoute 表示忽略特定的请求

   routes.MapRoute 表示为系统添加一条的规则,name表示路由名称是Default, url表示路由模式是{controller}/{action}/{id}, 括弧中表示一个"段",

   defaults表示路由默认值, 如果相应的段在请求Url中不存在但有默认值则取默认值 ,id 表示可选参数

   该条路由匹配以下url :  

      http://localhost/              controller = "Home", action = "Index"

      http://localhost/Home          controller = "Home", action = "Index"

      http://localhost/Home/Get              controller = "Home", action = "Get"

      http://localhost/Home/Get/1           controller = "Home", action = "Get", id = "1"

   假设有下一个场景,要查询某一分类某年某月某天的报表,我们设计Url模式为/Category/year/month/dd,具体的如下代码

      routes.MapRoute(

        name: "Report",
        url: "{Category}/{year}/{month}/{dd}",
        defaults: new { controller = "Report", action = "Index", year = UrlParameter.Optional,
          month = UrlParameter.Optional, dd = UrlParameter.Optional},
        constraints: new { method = new HttpMethodConstraint("GET"), year = @"\d+"}
     );

     这条路由包含了约束条件, 约束条件可以是正则表达式或实现了IRouteConstraint接口的对象

  2. 根据路由上下文件数据生成Url 

     UrlHelper.Action 和 HtmlHelper.ActonLink  分别根据controller/action生成Url和链接

    UrlHelper.RouteUrl 和HtmlHelper.RouteLink 根据特定路由生成Url和链接

    另外RouteCollection 两个静态属性影响生成Url

    LowercaseUrls  表示生成的Url是否小写形式

    AppendTrailingSlash 表示是否在生成的Url添加"/" 以标准化 

三. 路由系统其它特性

  1. 是否对物理文件路由 RouteCollection.RouteExistingFiles ,默认情况下为false

  2. 忽略特定的请求模式  通过IgnoreRoute方法注册相应的模式,该路由一个特殊的处理类,叫做StopRoutingHandler  

  3. 路由调试  路由调试组件需要另外安装,打开包管理控制台,执行 Install-Package routedebugger命令。通过该组件你可以在页面看到详细的路由匹配情况,另外安装会在Web.config中添加一个配置项<add key="RouteDebugger:Enabled" value="true" />, 设为false禁用路由调试

四. 区域路由(Area)

  ASP.NET MVC提供了区域功能帮助更好的组织网站结构,每个区域都是完整的ASP.NET MVC系统

  在大型Web系统中,也许要特定的模块组织网站逻辑架构, 比如一个订单管理系统,可以分成Customer,Products, Orders 等几个区域分别开发

五. 路由扩展

   通过实现抽象类RouteBase, IRouteHandler和IHttpHandler接口,可以对路由系统进行扩展,比如如下该列子

  http://www.cnblogs.com/luanwey/archive/2009/08/12/1544444.html 实现二级域名