Asp.net MVC 学习笔记 (2) --路由
这一篇主要介绍Asp.net MVC 路由。
如下为一些配置实例,以豆瓣为例,可以通过辅助工具RouteDebug调试:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //我的豆瓣 http://www.douban.com/people/3675683/notes routes.MapRoute("mine", "mine/", new { controller = "mine", action = "index" } ); //日志首页 http://www.douban.com/people/123123/notes routes.MapRoute("noteIndex", "people/{id}/notes", new { controller = "notes", action = "Index" }, new { id = @"^[0-9]*[1-9][0-9]*$", httpMethod = new HttpMethodConstraint("GET") } ); //匹配的参数为负整数 //routes.MapRoute("noteIndex1", "people/{id}/notes", // new { controller = "notes", action = "Index" }, // new { id = @"^((-\d+)|(0+))$" } // ); //日志详细 http://www.douban.com/note/66652523/ routes.MapRoute("noteShow", "note/{id}", new { controller = "notes", action = "Show" }, new { id = @"^\d+", httpMethod = new HttpMethodConstraint("GET", "POST") } ); //写日志 http://www.douban.com/note/create routes.MapRoute("noteWrite", "note/create", new { controller = "notes", action = "create" } ); //编辑日志 http://www.douban.com/note/edit/66652523 routes.MapRoute("noteEdit", "note/update/{id}", new { controller = "notes", action = "edit" }, new { id = @"^\d+" } ); //相册 http://www.douban.com/people/3675683/photos routes.MapRoute("albumIndex", "people/{id}/{controller}", new { controller = "mine", action = "index" }, new { id = @"^[0-9]*[1-9][0-9]*$", controller = @"(photos|miniblogs|board)" }//controller 为其中的一种 ); routes.MapRoute("albumEdit", "album/update/{id}.html", new { controller = "album", action = "update" }, new { id = @"^\d+" } ); routes.MapRoute("albumEdit1", "album/update/{id}_{classid}.html", new { controller = "album", action = "update" }, new { id = @"^\d+", classid = @"^\d+" } ); // Archive/2008-05-07/123.html 这个还没用判断 月和日的关系 routes.MapRoute( "archive", "archive/{date}/{id}.html", new { controller = "archive", action = "show" }, new { date = @"^\d{4}-\d{2}-\d{2}", id = @"^\d+" }); //复杂点的 http://www.cnblogs.com/news/2010/10/09/1846556.html routes.MapRoute( "news", "news/{year}/{month}/{day}/{id}.html", new { controller = "news", action = "show", year = "", month = "", day = "" }, new { id = @"^\d+", year = new YearRouteContraint(), month = new MonthRouteContraint(), day = new DayRouteContraint() }); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); RouteTable.Routes.RouteExistingFiles = true; // AreaRegistration.RegisterAllAreas(); // RegisterRoutes(RouteTable.Routes); }
其中自定义路由的类如下,这个到处都有:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Globalization; namespace MvcApplication1.UserRoutes { public class YearRouteContraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if ((routeDirection == RouteDirection.IncomingRequest) && (parameterName.ToLower(CultureInfo.InvariantCulture) == "year")) { try { int year = Convert.ToInt32(values["year"]); if ((year >= 1900) && (year <= 2100)) { return true; } return false; } catch (Exception ex) { return false; } } return false; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Globalization; namespace MvcApplication1.UserRoutes { public class MonthRouteContraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if ((routeDirection == RouteDirection.IncomingRequest) && (parameterName.ToLower(CultureInfo.InvariantCulture) == "month")) { try { int month = Convert.ToInt32(values["month"]); if ((month >= 1) && (month <= 12)) { return true; } return false; } catch (Exception ex) { return false; } } return false; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Globalization; namespace MvcApplication1.UserRoutes { public class DayRouteContraint:IRouteConstraint { #region IRouteConstraint 成员 public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if ((routeDirection == RouteDirection.IncomingRequest) && (parameterName.ToLower(CultureInfo.InvariantCulture) == "day")) { try { int month = int.Parse(values["Month"].ToString()); int day = int.Parse(values["Day"].ToString()); if (month <= 0 || month > 12) return false; if (day < 1) return false; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day < 32) return true; break; case 2: if (day < 29) return true; break; case 4: case 6: case 9: case 11: if (day < 31) return true; break; } } catch { return false; } } return false; } #endregion } }
当然您还可以在Asp.net WebForm中使用路由,这就不介绍了.