Asp.Net Mvc-路由实例及基本使用

ASP.NET MVC5路由系统机制

一、 Mvc的程序请求都会走路由系统,会映射到一个Controller对应的Action方法,而web form请求动态页面是会查找本地实际存在一个aspx文件。

*二、注册路由规则:(号表示匹配所有)

 //可以创建多条路由规则,但name属性不能相同
            //可以创建多条路由规则是有顺序的,如果被前面的路由规则匹配了,后面的就没机会了
            routes.MapRoute(
                name: "Default",
                //url: "Hotels/{controller}/{action}/{id}",//以Hotels开头
                url: "{controller}-{*action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                //约束
                constraints: new { controller = @"^\d+$" },    //以数字开头的控制器
                namespaces: new string[] { " MyFirstMvcManager.Controllers" }//去指定命名空间下搜索控制器
            );
            routes.MapRoute(
                name: "Default2",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

三、路由实例:

Demo:
在这里插入图片描述
1.RouteCollection routes 路由集规则合
2.Route对象:路由规则
3.RouteData:“Url地址匹配路由规则后解析出来的数据”
4.RouteTable:路由表(路由规则对象)

Mvc路由与管道结合:
在这里插入图片描述


更多MVC路由详细信息>>>

发布了83 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/MrLsss/article/details/104754134