知识 分享 互助 懒人建站

懒人建站专注于网页素材下载,提供网站模板、网页设计、ps素材、图片素材等,服务于【个人站长】【网页设计师】和【web开发从业者】的代码素材与设计素材网站。

懒人建站提供网页素材下载、网站模板
知识 分享 互助!

mvc5新特性RouteAttribute特征路由

作者:佳明妈 来源:博客园 2016-08-18 人气:4014 QQ交流群:4188479
mvc5新特性RouteAttribute特征路由,本文讲述如何开启mvc5的RouteAttribute路由功能并附上一个实例说明RouteAttribute是怎么工作的

mvc5新特性RouteAttribute特征路由,本文讲述如何开启mvc5的RouteAttribute路由功能并附上一个实例说明RouteAttribute是怎么工作的

1、mvc5的RouteAttribute概述

  RouteAttribute的命名空间是System.Web.Mvc,区别与web api的RouteAttribute(它的命名空间是System.Web.Http)

  默认情况下mvc的特征路由(RouteAttribute)功能是关闭的,需要手动启动,启动方式:

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

            routes.MapMvcAttributeRoutes();//启用Attribute路由

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

2、RouteAttribute简单实例

 
        [Route("list.html")]
        [Route("list_{category}.html")]
        [Route("list_{category}_p{page:int}.html")]
        public void ArticleList(string category, int page = 1)
        {
            var title = string.IsNullOrEmpty(category) ? "所有资讯" : category + "资讯";

            Response.Write(string.Format("分类:{0},页码:{1}<br/>我们都是好孩子!!!", title, page));
        }
 
  Route("list.html")匹配地址:http://localhost:2000/list.html

  mvc5新特性RouteAttribute特征路由

  Route("list/{category}.html")  匹配地址:http://localhost:2000/list/news.html

  路由匹配地址:http://localhost:2000/list/bews/2.html

  Route("list/{category}/{page:int}.html")  匹配地址:http://localhost:2000/list/bews/2.html
  

  路由匹配地址:http://localhost:2000/list/bews/2.html

↓ 查看全文

mvc5新特性RouteAttribute特征路由由懒人建站收集整理,您可以自由传播,请主动带上本文链接

懒人建站就是免费分享,觉得有用就多来支持一下,没有能帮到您,懒人也只能表示遗憾,希望有一天能帮到您。

试玩游戏,也能赚钱

1块钱也能提现

→立即行动

mvc5新特性RouteAttribute特征路由-最新评论