我是靠谱客的博主 坚强热狗,这篇文章主要介绍mvc5新特性RouteAttribute特征路由,现在分享给大家,希望可以做个参考。

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)功能是关闭的,需要手动启动,启动方式:

复制代码
1
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简单实例

 
复制代码
1
2
3
4
[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)); }
 
复制代码
1
  Route("list.html")匹配地址:http://localhost:2000/list.html

  mvc5新特性routeattribute特征路由

复制代码
1
2
  Route("list/{category}.html") 匹配地址:http://localhost:2000/list/news.html

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

复制代码
1
2
3
  Route("list/{category}/{page:int}.html") 匹配地址:http://localhost:2000/list/bews/2.html   

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

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

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

转载于:https://www.cnblogs.com/cjm123/p/8671730.html

最后

以上就是坚强热狗最近收集整理的关于mvc5新特性RouteAttribute特征路由的全部内容,更多相关mvc5新特性RouteAttribute特征路由内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部