<1>
进入到Global.asax文件,我们看到RouteConfig.RegisterRoutes(RouteTable.Routes); 执行了这段代码,这段代码就是注册路由的
我们来看看这个RouteTable
namespace System.Web.Routing
{
// 摘要:
// 存储应用程序的 URL 路由。
[TypeForwardedFrom("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
public class RouteTable
{
// 摘要:
// 初始化 System.Web.Routing.RouteTable 类的新实例。
public RouteTable();
// 摘要:
// 获取从 System.Web.Routing.RouteBase 类派生的对象的集合。
//
// 返回结果:
// 包含集合中的所有路由的对象。
public static RouteCollection Routes { get; }
}
}
我们再来看看这个RouteCollection类的MapRoute扩展方法,在这个扩展方法中构建了路由
/// <summary>映射指定的URL路由并设置默认路由值、约束和命名空间</summary>
/// <returns>对映射路由的引用</returns>
/// <param name="routes">应用程序的路由集合。</param>
/// <param name="name">要映射的路线的名称。</param>
/// <param name="url">路由的URL模板</param>
/// <param name="defaults">默认的路由名称</param>
/// <param name="constraints">一组指定值的表达式 <paramref name="url" /> parameter.</param>
/// <param name="namespaces">应用程序的命名空间。</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="routes" /> or <paramref name="url" /> parameter is null.</exception>
public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
{
//这是一个路由集合类RouteCollection的扩展方法
if (routes == null)
{
throw new ArgumentNullException("routes");
}
if (url == null)
{
throw new ArgumentNullException("url");
}
Route route = new Route(url, new MvcRouteHandler()) //构造路由,这个路由的Handler是MVCRouteHanlder
{
/*
我们来看看这个Route类的构造函数(这段代码是我特意放到这里的,目的是方便看)
public Route(string url, IRouteHandler routeHandler)
{
this.Url = url;
this.RouteHandler = routeHandler;
}
*/
Defaults = RouteCollectionExtensions.CreateRouteValueDictionary(defaults),
Constraints = RouteCollectionExtensions.CreateRouteValueDictionary(constraints),
DataTokens = new RouteValueDictionary()
};
if (namespaces != null && namespaces.Length > 0)
{
route.DataTokens["Namespaces"] = namespaces;
}
routes.Add(name, route); //将构造的路由加入到路由集合当中去
return route;
}
以上就是构建路由,将路由加添加路由集合中去
我们在来看看路由的自定义约束
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 },
constraints: new { id = new MyRote() } //这一种就是使用自定义约束
//constraints:new{id =@"\d"}, 这一种是使用字符串的string的方式进行约束 ,@"\d"就是一个string
);
}
}
public class MyRote : IRouteConstraint //自定义约束必须继承自IRouteConstraint接口
{
//在这里实现以下IRouteConstraint接口的Match方法
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
//在这里我们可以对我们的参数进行自定义。例如在这里我们要限定我们的Id参数必须为整数
//假设我们请求这个地址:http://localhost:8447/Home/Index/8
var id = values[parameterName].ToString(); //parameterName表示参数的名称
if (id.Length > 0)
{
var boo = Regex.IsMatch(id, "\\d");
if (boo == false)
{
return false;
}
}
return true;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication
{
public class RouteConfig
{
//用户将请求发送到后台的一个服务器,首先经过路由模块,路由模块会根据以下的路由规则对你请求的URL地址做一个解析,解析出你请求的控制器是哪个,Action方法是哪个,其他参数是什么。路由模块解析完后在到达具体的控制器,控制器再调用相关的Action方法,Action被调用之后,可能会访问我们的业务逻辑层Model。等业务逻辑层返回相关数据之后。控制器再把这个数据交给View视图【控制器交给View的数据我们一般叫做ViewModel】。View就将这个ViewModel数据做一个组装。生成HTML代码发送给用户。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default", //作为路由规则的key,因为可以有多条路由规则,所以这个key就是一条路由规则的唯一标识,是一定不能重复的。
url: "{controller}/{action}/{id}",//请求后台的Url规则,即URL地址的格式。{}就代表了一个占位符。{controller}就表示这个占位符里的内容是一个控制器的名字{action}就代表这个占位符里的内容是一个Action方法的名字。{id}就代表了参数,可以写,也可以不写。所以这条路由的URL地址的规则就是 http://localhost:2685/Home/Index
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } //默认值,也就是说当你没有给他Url规则的时候,他默认的控制器名字为“Home” Action名字为“Index” id的默认为:它会根据你后台这个id的类型会自动给你装配一个具体的默认值。比如说你后台的id是一个int类型,那我就给你装配一个0,如果你后台的id是一个string类型的,那我就给你装配一个""
//这个是一个约束,里面添加了一个控制器的约束和一个id参数的约束,让它们符合一个规定的正则表达式。
//constraints: new { controller=@"^\w+$",id=@"^\d+$"},
//限定我们搜索的这个Mvc控制器必须在某个名称控件下去搜索。这样的话就能够比较快的搜索到我们要的控制器
//这里是限定在MvcRazorDome.Controllers名称控件下去搜索控制器
//namespaces:new string[]{"MvcRazorDome.Controllers"}
);
//---------------------------------------------------------------第二个路由规则
routes.MapRoute(
name: "Default2",
url: "{controller}-{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//constraints: new { controller = @"^\w+$", id = @"^\d+$" },
//namespaces: new string[] { "MvcRazorDome.Controllers" }
);
}
}
}