1. URLRouting简介

   URL(Uniform Resource Locator),统一资源定位器,是用于完整描述Internet上的网页或其他资源地址的一种标识方法。

  URL一般可以由6部分组成,格式如下:

protocol :// hostname [:port] [/path] [?parameters] [#fragment]

  URL各部分说明:

  protocol 协议:可以是HTTP(超文本传输协议)、FTP(文件传输协议)和HTTPS(安全超文本传输协议)。

  hostname 主机名:指在互联网中存放资源的服务器DNS主机名或IP地址。

  port 端口号:该选项是一个小于66536的正整数,是各服务器或协议约定的通信端口。

  path 路径:用来表示一个Web站点中的目录或文件资源的地址。

  parameters 参数列表:参数形式为以=隔开的键/值对,多个参数之间用&连接。

  fragment 信息片段:用于直接定位到页面中的某个锚点标记。

2. URLRouting与URLRewrite区别

  URLRouting是一组从URL到请求处理程序间的映射规则,将URL映射到能够处理业务需求的Action上。URLRouting是一个独立的类库System.Web.Routing.dll。

  URLRouting为将URL映射到Controller的Action上,处理流程图如下:

  URLRewrite为将URL映射到具体的文件资源上,处理流程图如下:

3. ASP.NET MVC中使用及自定义URLRouting规则

  在Web.config文件中与Routing有关的的节点:sytem.web.httpModules,system.web.httpHandlers,system.webserver.modules,system.webserver.handlers。

  ASP.NET MVC应用程序第一次启动时,将调用Global.asax中Application_Start()方法。每个ASP.NET MVC应用程序至少需要定义一个URLRouting来指明应用程序如何处理请求,复杂的应用程序可以包含多个URLRouting。

3.1 App_Start/RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;namespace Libing.Portal.Web
{public class RouteConfig{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 });}}
}

  Global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;namespace Libing.Portal.Web
{public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){AreaRegistration.RegisterAllAreas();RouteConfig.RegisterRoutes(RouteTable.Routes);}}
}

3.2 Route类

  RouteCollection对象以静态属性的方式声明在RouteTable的属性Routes中,RouteCollection对象存储的是Route类的实例。一个完整的Route类实例需要有URL、默认值、约束、数据密钥及路由处理程序等属性。

public RouteValueDictionary Constraints { get; set; }
public RouteValueDictionary DataTokens { get; set; }
public RouteValueDictionary Defaults { get; set; }
public IRouteHandler RouteHandler { get; set; }
public string Url { get; set; }

3.3 Route类属性

  name:
  路由名称,必须是唯一不能重复。

  url:
  在Route类中,属性URL是一个字符串,用于描述请求中URL的格式。该字符串可能不完全是一个实际的URL,可以带一些{}标记的占位符,使用占位符可以从URL中提取数据。如:

"{controller}/{action}/{id}"

  {controller}参数的值用于实例化一个处理请求的控制类对象,{action}参数的值用于指明处理当前请求将调用控制器中的方法。

  defaults:

new { controller = "Home", action = "Index", id = UrlParameter.Optional }

  constraints:

new { controller = @"^\w+", action = @"^\w+", id = @"\d+" }

  namespaces:

  Route.DataTokens属性,获取或设置传递到路由处理程序但未用于确定该路由是否匹配 URL 模式的自定义值。

3.4 自定义URLRouting规则

  分页:

routes.MapRoute("Page","{controller}/List/Page/{page}",new { controller = "Home", action = "List", page = UrlParameter.Optional },new { page = @"\d*" });

public string List(int? page){return page == null ? "1" : page.ToString();}

  本地化多语言Routing:

public static void RegisterRoutes(RouteCollection routes)
{routes.IgnoreRoute("{resource}.axd/{*pathInfo}");// 本地化
    routes.MapRoute(name: "Localization",url: "{lang}/{controller}/{action}/{id}",defaults: new { lang = "zh-CN", controller = "Home", action = "Index", id = UrlParameter.Optional },constraints: new { lang = "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$" });routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

  分页Routing:

routes.MapRoute(name: "PagedList",url: "{controller}/Page/{page}",defaults: new { controller = "Product", action = "Index" },constraints: new { page = @"\d+" }
);

  Blog根据日期Routing:

routes.MapRoute(name: "blog",url: "blog/{user}/{year}/{month}/{day}",//defaults: new { controller = "Blog", action = "Index", day = 1 },defaults: new RouteValueDictionary{{"controller", "Blog"},{"action", "Index"},{"day", 1}},constraints: new { year = @"\d{4}", month = @"\d{1,2}", day = @"\d{1,2}" }
);

  Reports根据年月Routing:

routes.MapRoute(name: "Reports",url: "Reports/{year}/{month}",defaults: new { controller = "Reports", action = "Index" },constraints: new { year = @"\d{4}", month = @"\d{1,2}" }
);

3.5 创建Routing约束

  使用正则表达式来指定路由约束:

routes.MapRoute(name: "Product",url: "Product/{ProductID}",defaults: new { controller = "Product", action = "Details" },constraints: new { ProductID = @"\d+" }
);

3.6 自定义Routing约束

  通过实现IRouteConstraint接口来实现自定义路由。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;using System.Web.Routing;namespace Libing.Portal.Web.Models.Constraints
{public class LocalhostConstraint : IRouteConstraint{public bool Match(HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection){return httpContext.Request.IsLocal;}}
}

routes.MapRoute(name: "Admin",url: "Admin/{action}",defaults: new { controller = "Admin" },constraints: new { isLocal = new LocalhostConstraint() }
);

  自定义浏览器访问Routing约束:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;using System.Web.Routing;namespace Libing.Portal.Web.Models.Constraints
{public class UserAgentConstraint:IRouteConstraint{private string _userAgent;public UserAgentConstraint(string userAgent){_userAgent = userAgent;}public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){return httpContext.Request.UserAgent != null && httpContext.Request.UserAgent.Contains(_userAgent);}}
}

routes.MapRoute(name: "Chrome",url: "{*catchall}",defaults: new { controller = "Home", action = "Index" },constraints: new { customConstraint = new UserAgentConstraint("Chrome") }
);

  自定义用户个人网址Routing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;using System.Web.Routing;using Libing.Portal.Web.Models;namespace Libing.Portal.Web.Models.Constraints
{public class UserConstraint : IRouteConstraint{public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){using (PortalContext context = new PortalContext()){string userRouteValue = values["user"].ToString();var user = (from u in context.Userswhere u.UserName == userRouteValueselect u).FirstOrDefault();return user != null;}}}
}

routes.MapRoute(name: "User",url: "{user}/{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },constraints: new { isValidUser = new UserConstraint() }
);

4. 使用RouteDebugger调试URLRouting

  RouteDebugger为一个独立的类库,RouteDebug.dll,可以从网上下载到,使用方法如下:

  1>. 添加对RouteDebug引用;

  2>. Global.ascx修改

 using RouteDebug;

protected void Application_Start() {   AreaRegistration.RegisterAllAreas();

   RegisterGlobalFilters(GlobalFilters.Filters);   RegisterRoutes(RouteTable.Routes);

   RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); // 添加RouteDebug}

  附件:RouteDebug.rar

转载于:https://www.cnblogs.com/libingql/archive/2012/04/07/2435687.html

ASP.NET MVC系列:UrlRouting相关推荐

  1. 学习ASP.NET MVC系列 - 还有比这更简炼的吗?把复杂的事情变简单了,贡献啊!...

    转自 学习ASP.NET MVC系列: 学习ASP.NET MVC(一)--我的第一个ASP.NET MVC应用程序 学习ASP.NET MVC(二)--我的第一个ASP.NET MVC 控制器 学习 ...

  2. (转)一步一步Asp.Net MVC系列_权限管理设计起始篇

    原文地址:http://www.cnblogs.com/mysweet/archive/2012/07/26/2610793.html 前一段时间,写了一步一步asp.net的一系列博客,最近,也快要 ...

  3. ASP.NET MVC系列:添加控制器

    基于MVC的应用程序包含三个部分 Models(模型):对应用程序的数据进行处理 Views(视图):动态生成HTML,显示数据 Controllers(控制器):应用程序中处理用户交互的部分,处理浏 ...

  4. 【.NET特供-第三季】ASP.NET MVC系列:传统WebForm站点和MVC站点执行机制对照

    本文以图形化的方式,从'执行机制'方面对照传统WebForm站点和MVC站点. 请參看下面图形: 一.执行机制 当我们訪问一个站点的时候,浏览器和server都是做了哪些动作呢? (本文仅仅是提供一个 ...

  5. (转)一步一步Asp.Net MVC系列_权限管理之权限控制

    原文地址:http://www.cnblogs.com/mysweet/archive/2012/08/05/2623687.html 在权限管理中一个很重要的就是关于权限的拦截验证问题,特别是我们在 ...

  6. 一步一步Asp.Net MVC系列_权限管理总结(附MVC权限管理系统源码)

    TZHSWEET:请大家多多反馈问题,我已经在修改中了,已更新版本...... 如果大家遇到数据库附加问题,EF连接字符串问题,请自行配置,如果有bug反馈可以私聊,我的qq:409180955. 项 ...

  7. ASP.NET MVC Framework 系列

    序言 做为设计模式的王者,MVC在众多领域都成为良好的模型的代名词,从前在ASP.NET下我们只能依靠Monorail来实现ASP.NET下无控件的MVC,但是现在ASP.NET 下的MVC已经成为现 ...

  8. ASP.NET MVC 重点教程一周年版 第二回 UrlRouting 【转】

    一.什么是UrlRouting 你可以使用UrlRouting来配置一些URL的映射,使用户可以按你的规则来访问网站. 使用UrlRouting,一定要规定URL模式,它包括一个位置标识,它将在你请求 ...

  9. 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC

    七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 系列文章 七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)-- ...

最新文章

  1. AIX下两个常用命令
  2. Runnable接口介绍(中文文档)
  3. AOV网拓扑排序(c/c++)
  4. Java运算符优先级(转)
  5. 脚本调试工具 Microsoft Script Debugger
  6. python提取文件指定列_如何从csv文件中提取特定列并使用python绘图
  7. 建筑学跨专业计算机考研方向,不适合女生报考的考研专业你知道几个?
  8. Hyper-V 2016 系列教程28 Hyper-v平台USB 外设解决方案介绍
  9. 从技术和历史的视角,理解Too many technical terms in FE BE
  10. 台达b3伺服modbus通讯_A2伺服modbus通讯难题-专业自动化论坛-中国工控网论坛
  11. 制作png格式透明图片的简易方法
  12. linux 检查系统丢包,Linux系统网络丢包问题解决
  13. SharePoint 2019 部署(二)搜索
  14. SNF开发平台-SNF.CodeGenerator-升级生成BS页面代码-支持视图-数据库配置-快速开发者的利器...
  15. Supplementary Materials for The spread of true and false news online 解读
  16. cocos2d-x 添加启动数字输入法的功能
  17. 数据库 redis
  18. 64个数据分析常用术语
  19. 漫威MARVEL漫画官方能力参考
  20. c语言基础知识选择题,C语言基础知识选择试题.doc

热门文章

  1. 基于zookeeper实现的分布式锁
  2. 当期收益率(Current Yield)
  3. Ocelot简易教程(七)之配置文件数据库存储插件源码解析
  4. ACM Doing Homework again
  5. Linux2017Linux架构师!
  6. oracle 工作中遇到的函数
  7. Codeforces Beta Round #7 C. Line (扩展欧几里德)
  8. spoj 375 Query on a tree (树链剖分)
  9. Discuz! X2.5 添加自定义数据调用模块
  10. jwPlayer实现支持IE8及以下版本避免出错的方法