我们要实现
通过路由 匹配出 博客地址 和博客文章地址
例如下面的这两个地址
//http://www.cnblogs.com/maijin/
//http://www.cnblogs.com/maijin/archive/2009/01/12/1374473.html
通过路由配置 让控制器能处理 用户的不同提交
第一步 写默认路由规则
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //路由 其实 说白了 就是 怎么从用户提交的网址 匹配出 那个控制器controller 来处理这个请求 //http://www.cnblogs.com/maijin/ //routes.MapRoute("Blog", "{controller}/{index}/{name}", new { controller = "Blog", action = "Index" }); routes.MapRoute("Blog", "{name}", new { controller = "Blog", action = "Index" }); //http://www.cnblogs.com/wintersun/archive/2009/01/12/1374473.html //routes.MapRoute("Archive", "{name}/{controller}/{year}/{month}/{day}/{id}.html", new {controller = "Archive",action = "Index",year = @"\d4",month = @"\d2",day = @"\d2",id = @"\d+"}); routes.MapRoute("Archive", "{name}/archive/{year}/{month}/{day}/{id}.html", new { controller = "Archive", action = "Index", year = @"\d4", month = @"\d2", day = @"\d2", id = @"\d+" }); routes.MapRoute("DataTime", "p/{datatime}", new { controller = "Archive", action = "DataTimeText" }); //http://localhost:64301/ //http://localhost:64301/home/ //http://localhost:64301/home/index //http://localhost:64301/home/index/12 //controller = null action = null id = routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }
第二步 控制器处理
public class BlogController : Controller { // // GET: /Blog/ public ActionResult Index(string name) { ViewData["name"] = name; return View(); } }
第三步 视图
@{ ViewBag.Title = "Index"; } <h2>@ViewData["name"].ToString()</h2>
视频教程 下载
http://pan.baidu.com/share/link?shareid=1571916703&uk=3576826227
源码下载
http://www.bamn.cn/thread-1150-1-1.html#source.rar