mvc5新特性RouteAttribute特征路由

1 篇文章 0 订阅

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值