MVC 自定义路由规则

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;


namespace FirstMVC
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name:"NewsShow",
                url:"{year}-{month}-{day}-{id}",
                defaults:new {controller="News",action="Show"},
                constraints:new
                {
                    year="^\d{4}$",
                    month = "^\d{1,2}$",
                    day = "^\d{1,2}$",
                }
                );



            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Hello", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace FirstMVC.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/


        public ActionResult Index()
        {
            return View();
        }

        //2014-1-1-5
        public ActionResult Show(int id, int year, int month, int day)
        {
            ViewBag.id = id;
            ViewBag.year = year;
            ViewBag.month = month;
            ViewBag.day = day;

            return View();
        }


    }
}

原文地址:https://www.cnblogs.com/dxmfans/p/9434750.html