Asp.Net MVC advanced articles blog: implementation path and the path matching ro

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

We want to achieve

By routing matching blog and blog posts.

For example, the two address below

//http://www.cnblogs.com/maijin/

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

By routing configuration let controller can handle different user submitted




The first step is to write the default routing rules

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //Routing in fact is how the user submitted URL matching the controller controller to handle the request
           
            //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
            );

        }

The second step controller


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

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

    }

Third step view

@{
    ViewBag.Title = "Index";
}

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

Video tutorial Download

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

Download the source code

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lou at November 14, 2013 - 4:23 AM