• 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();
            }


        }
    }

  • 相关阅读:
    【Java编程思想】13.字符串
    【Java编程思想】12.通过异常处理错误
    【Java编程思想】10.内部类
    【Java编程思想】11.持有对象
    【Java编程思想】9.接口
    【Java编程思想】8.多态
    【Java编程思想】7.复用类
    【Java编程思想】6.访问权限控制
    【Java编程思想】4.控制执行流程
    Ribbon、Feign、Hystrix使用时的超时时间设置问题
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434750.html
Copyright © 2020-2023  润新知