最近一直在学习mvc,其中对于 Url.Action生成的url感到很困惑。官方的解释的基于路由方案生成的url。问题是,怎样基于,怎样选择,没有过多的解释。网上找了很多资料,也看不懂,最后还是在pro asp.net mvc3 framework这本书看明白了。

我的路由方案是这样的

  public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute
(
null,
"",
new { controller = "Product", action = "List", category = (string)null, page = },
new { page = @"\d+" }
); routes.MapRoute
(
null,
"Page{page}",
new { controller = "Product", action = "List", category = (string)null },
new { page = @"\d+" }
); routes.MapRoute
(
null,
"{category}",
new { controller = "Product", action = "List", page = } ); routes.MapRoute
(
null,
"{category}/Page{page}",
new { controller = "Product", action = "List" },
new { page = @"\d+" }
); routes.MapRoute(
name: null,
url: "{controller}/{action}/{id}",
defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
); }

路由方案

这是我需要输出的设置

 <div class="pager">
@Html.PagingLinks(Model.pagingInfo, x => Url.Action("List", new { controller = "Product", action= "List", category = Model.CurrentCategory, page = x }))
</div>

请各位注意传入的匿名类型的那几个属性

new {
              controller = "Product",
              action= "List",
              category = Model.CurrentCategory,
              page = x })
              )

最终它匹配

routes.MapRoute
                (
                null,
                "{category}/Page{page}",
                new { controller = "Product", action = "List" },
                new { page = @"\d+" }
                );

这条路由,生成的url就像是这样

http://localhost:2154/球类/Page2

接下来就是要解释为什么会这样呢。

生成url的原则(对书的总结哈),我自己的总结是:

1、明个片段名必须都得到匹配(有默认值的,而你提供的匿名参数可以没有该变量)例如

routes.MapRoute(
                name: null,
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Product", action = "List" }
            );

这条路由你只需提供new{id=5}就能匹配到了

2、如果片段名满足了,那只读默认量要么没有,要么必须一模一样,例如

routes.MapRoute
            (
             null,
             "{category}",
              new { controller = "Product", action = "List", page = 1 }

);

那么当你提供的匿名参数的时候,page参数要么没有,有的话必须是1,否则直接不匹配,哪怕你已经匹配了category这个片段量

3、基于路由方案生成的url不是以最佳路由生成,而是以最先找到生成,例如:

 routes.MapRoute(
name: "fist",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
);
routes.MapRoute
(
"second",
"{category}/Page{page}",
new { controller = "Product", action = "List" },
new { page = @"\d+" }
);

我把first这条路由提前了,那么我得到的结果是:

http://localhost:2154/?category=球类&page=2

为什么会得到这个结果呢?

这是因为它先匹配是fist这条路由,它满足{controller}/{action}和片段量,并且id是可有可无所以,直接匹配它,直接放回,剩下的参数category和page以?查询字符串出现。所以越具体的路由应该放在前面,越抽象的应该放到后面

4,符合各种约束,例如正则表达式什么的。

以上就是我对mvc生成url的理解,@Html.ActionLink方法也是一样的。

第一次写博客请见谅哈。

mvc 生成输出url的更多相关文章

  1. 生成输出url时,使用CSS来控制超链接的格式

    在前文<生成输出url>中的第5点,介绍了使用ActionLink生成输出url中指定html标签属性. 例如, 假设Global.asax中的路由定义为: public static v ...

  2. 生成输出 URL(16.2)

    1.在视图中生成输出 URL 几乎在每一个 MVC 框架应用程序中,你都会希望让用户能够从一个视图导航到另一个视图 —— 通常的做法是在第一个视图中生成一个指向第二个视图的链接,该链接以第二个视图的动 ...

  3. 生成输出url

    继续使用前面的例子11-3URLTestDemo,修改Global.asax中的RegisterRoutes方法如下: public static void RegisterRoutes(RouteC ...

  4. 生成链接中的全限定URL(Generating Fully Qualified URLs in Links) | 在视图中生成输出URL | 高级路由特性

    结果:<a class="myCSSClass"href="https://myserver.mydomain.com/Home/Index/MyId#myFrag ...

  5. 指定HTML标签属性 |Specifying HTML Attributes| 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼

    结果呢: <a class="myCSSClass" href="/" id="myAnchorID">This is an o ...

  6. 传递额外的值 Passing Extra Values |在视图中生成输出URL | 高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼

    结果呢 <a href="/App/DoCustomVariable?id=Hello">This is an outgoing URL</a> 理解片段变 ...

  7. 用路由系统生成输出URL 在视图中生成输出URL 高级路由特性 精通ASP-NET-MVC-5-弗瑞曼

    Using the Routing System to Generate an Outgoing URL 结果呢:<a href="/Home/CustomVariable" ...

  8. 根据指定路由生成URL |Generating a URL from a Specific Route | 在视图中生成输出URL|高级路由特性

    后面Length=5 是怎么出现的?

  9. 在动作方法中生成输出URL (Generating Outgoing URLs in Action Methods) |

随机推荐

  1. 【转】Could not write file XXX\.classpath解决

    原文网址:http://www.sjsjw.com/kf_other/article/323_11877_12218.asp 环境 MyEclipse 8.6 + Windows 7 Ultimate ...

  2. 【数学】XMU 1597 GCD

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1597 题目大意: 求(am-bm, an-bn),结果取模1000000007,a,b ...

  3. winPcap_4_获取已安装设备的高级信息

    由 pcap_findalldevs_ex() 返回的每一个 pcap_if 结构体,都包含一个 pcap_addr 结构体,这个结构体由如下元素组成: 一个地址列表 一个掩码列表 (each of ...

  4. oracle管道输出

    通常我们会在oracle中用dbms_output输出调试信息,但dbms_output只能在调用过程完成才返回结果,不能实时输出的.这意味着通常我们经常要等几分钟或更长的时间才能看到调试信息,那怎么 ...

  5. GDB错误:Cannot find bounds of current function

    http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...

  6. [置顶] Hash查找,散列查找

    //Hash.h #ifndef HASH_H #define HASH_H #define HASH_ARR_SIZE 100 #define FILL -1 #include <stdlib ...

  7. CentOS 6.3下rsync服务器的安装与配置[转]

    CentOS 6.3下rsync服务器的安装与配置   一.rsync 简介 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也 ...

  8. gzip优化网络传输量提高传输效率[转]

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;us ...

  9. iPhone手机GPS地图位置好帮手

    十一国庆黄金周近在眉睫,我先祝大家过一个愉快开心的国庆长假. 假期内,难免老友聚会吃饭聊天联络感情,年轻朋友相亲约会,一家人出门旅游.平时,我们聚会时,总有要来的人找不到聚会地点,需要反复打电话确认: ...

  10. C#3.0 语言基础扩充

    隐含类型局部变量 var i = 5; var h = 13.4; var s = "C Sharp"; var intArr = new[] {1,2,3 }; var a = ...