MVC的路由系统
在ASP.NET中,每一个URL都与一个文件相对应,但是在MVC中是由Controller中的Action相对应的。这是因为在MVC中使用了路由系统,路由系统不是MVC特色,它原本就存在于ASP.NET,在ASP.NET中也可以实现以路由的方式请求每一个.aspx页面。下面我们来做一个示例,首先创建一个ASP.NET的web项目,添加Teacher和Student两个类,代码如下:
public class Teacher { public int Tid { get; set; } public string Name { get; set; } public string Subject { get; set; } } public class Student { public int ID { get; set; } public string Name { get; set; } public string Class { get; set; } public int Tid { get; set; } }
添加Data类充当数据存储来初始化数据:
View Code
public class Data { public static List<Teacher> arrayTeacher; public static List<Student> arrayStudent; static Data() { arrayTeacher = new List<Teacher> { new Teacher{ Tid=1, Name="苏娜", Subject="英语"}, new Teacher{ Tid=2, Name="刘宇",Subject="语文"}, new Teacher{ Tid=3,Name="周文",Subject="数学"} }; arrayStudent = new List<Student> { new Student{ ID=1, Class="1班", Name="张三", Tid=1}, new Student{ID=2,Class="1班",Name="李四",Tid=2}, new Student{ID=3,Class="2班",Name="王五",Tid=2}, new Student{ID=4,Class="2班",Name="魏六",Tid=3} }; } /// <summary> /// 获取所有的老师 /// </summary> /// <returns></returns> public List<Teacher> GetTeachers() { return arrayTeacher; } /// <summary> /// 查找老师对应的学生 /// </summary> /// <param name="Tid"></param> /// <returns></returns> public List<Student> GetStudent(int Tid) { return arrayStudent.Where(s => s.Tid == Tid).ToList(); } }
添加Default.aspx页面,代码如下:
View Code
//页面部分 <asp:Repeater ID="RpTeacher" runat="server"> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Tid","~/Student/{0}") %>' Font-Overline="false"> <%# Eval("Name")%> </asp:HyperLink> </ItemTemplate> </asp:Repeater> //后台部分 private Data teacherAndStudent; public Data TeacherAndStudent { get { return null == teacherAndStudent ? teacherAndStudent = new Data() : teacherAndStudent; } } protected void Page_Load(object sender, EventArgs e) { RpTeacher.DataSource = TeacherAndStudent.GetTeachers(); RpTeacher.DataBind(); }
添加Student.aspx页面,代码如下:
View Code
//页面部分 <asp:Repeater ID="rpStudent" runat="server"> <ItemTemplate> <asp:Label><%# Eval("Name") %> </asp:Label> </ItemTemplate> </asp:Repeater> //后台部分 private Data teacherAndStudent; public Data TeacherAndStudent { get { return null == teacherAndStudent ? teacherAndStudent = new Data() : teacherAndStudent; } } protected void Page_Load(object sender, EventArgs e) { int tid = Convert.ToInt32(this.RouteData.Values["tid"]); rpStudent.DataSource = TeacherAndStudent.GetStudent(tid); rpStudent.DataBind(); }
之后运行程序进入Default页面,在Default页面中点击一个老师进入Student页面并观察URL地址。
MVC的路由主要有两个用途:1、对比URL请求并判断是由哪个Controller和Action处理。2、输出URL。那么先看一下Global.asax文件
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // 路由名称 "{controller}/{action}/{id}", // 带有参数的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); }
Application_Start方式是整个程序运行的第一个方法,它是整个程序的入口,所有的MVC Routing都会在这里定义,RouteTable.Routes是一个公开的静态对象,用于存储所有的Routing的规则集(RouteCollection类)。RegisterRoutes方法中的IgnoreRoute方法用于定义不需要通过Routing处理的网址。{resource}代表一个名为"resource"的RouteValueg表达式,在这里去任何名字都可以。{{*pathInfo}}代表一个名为“pathInfo”的RouteValue表达式,"pathInfo"的RouteValue表达式的值是一个完整的路径信息中除去"resource"后所剩余的部分。MapRoute方法是最常用来定义Routing规则的辅助方法。

我们还可以对网址路由加入限制条件,例如下面对ID做限制,让它只能为数字,当然你可以用正则表达式规定任何你想要的参数约束。
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new { id = @"\d+" } ); }

浙公网安备 33010602011771号