Asp.Net MVC 進階篇:路由匹配 實現博客路徑 和文章路徑

咱們要實現
經過路由 匹配出 博客地址 和博客文章地址html

例以下面的這兩個地址spa

//http://www.cnblogs.com/maijin/
//http://www.cnblogs.com/maijin/archive/2009/01/12/1374473.html3d

經過路由配置 讓控制器能處理 用戶的不一樣提交code

 

 

 

第一步 寫默認路由規則視頻

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
            );

        }

 

第二步 控制器處理htm

 

    public class BlogController : Controller
    {
        //
        // GET: /Blog/

        public ActionResult Index(string name)
        {
            ViewData["name"] = name;
            return View();
        }

    }

第三步 視圖blog

@{
    ViewBag.Title = "Index";
}

<h2>@ViewData["name"].ToString()</h2>

視頻教程 下載教程

http://pan.baidu.com/share/link?shareid=1571916703&uk=3576826227路由

源碼下載get

http://www.bamn.cn/thread-1150-1-1.html#source.rar

相關文章
相關標籤/搜索