zoukankan      html  css  js  c++  java
  • MVC MVC 路由详解

    在项目中我们引用了System.Web.Routing;   Routing的作用:

    确定Controller

    确定Action

    确定其他参数

    根据识别出来的数据, 将请求传递给Controller和Action.

    Global.asax中

    在App_Start目录中的RouteConfig.cs文件

    注册一条路由规则

    name 参数:

       规则名称, 可以随意起名.不可以重名,否则会发生错误,路由集合中已经存在名为 “Default” 的路由。路由名必须是唯一的。 

    url 参数:

     url获取数据的规则, 这里不是正则表达式, 将要识别的参数括起来即可, 比如: {controller}/{action}

    defaults 参数:

     url参数的默认值. 我们只建立了一条url获取数据规则: {controller}/{action}  那么这时就会为action参数设置defaults参数中规定的默认值. new { controller = "Home", action = "Index" }

    多条路由规则

     

    RouteDebug.dll

    RouteDebug.dll调试类库放入packages包下,packages文件夹下包含项目用到的所有第三方库。 然后添加引用RouteDebug.dll到项目中。

    在Global.asax.cs注册路由之后添加代码:

    RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);   重写了路由调试的地址,将已经注册了的路由规则进行测试。

    例如:

      //从繁到简的写法,参数多的写前面/难匹配的写前面   多个路由设计
    
              //  routes.MapRoute(
              //name: "FF",
              //url: "{controller}-{action}-{id}-{name}",
              //defaults: new { controller = "Home", action = "Index" }
              //);
    
              //  routes.MapRoute(
              //  name: "DD",
              //  url: "{controller}-{action}-{id}",
              //  defaults: new { controller = "Home", action = "Index"}
              //  );
    
              //  routes.MapRoute(
              //  name: "SS",
              //  url: "{controller}-{action}",
              //  defaults: new { controller = "Home", action = "Index"}
              //  );          
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

    启用测试路由,不管你打开那个Action都会跳到它的测试页面去 

      RouteConfig.RegisterRoutes(RouteTable.Routes); //注册路由
                BundleConfig.RegisterBundles(BundleTable.Bundles);
                //RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); //测试路由
  • 相关阅读:
    利用条件变量实现生产者消费者模型
    加密算法
    brk mmap malloc使用
    c++中的RTTI机制
    std::array vector 数组联系和区别
    Intent基本使用
    [WPF]DataGrid C#添加右键弹出选择菜单
    c# 通过解析mp3规范命名并上传服务器
    自定义ComboBox,简简单单实现
    自定义窗体,简简单单实现
  • 原文地址:https://www.cnblogs.com/Sea1ee/p/5978561.html
Copyright © 2011-2022 走看看