zoukankan      html  css  js  c++  java
  • MVC5新特性(一)之RouteAttribute打造自己的URL规则

    1、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

      

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

      

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

      

    
    
  • 相关阅读:
    HTML Style Sheet
    Chrome 崩溃 相关
    android dialog 不变暗
    URL replacement
    android SharedPreferences
    android PreferenceActivity
    据说还可以的网站
    android brightness control
    Android network status 3G/WIFI
    android activity onSearchRequested()
  • 原文地址:https://www.cnblogs.com/amywechat/p/5767619.html
Copyright © 2011-2022 走看看