惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
S
Securelist
P
Proofpoint News Feed
D
DataBreaches.Net
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Project Zero
Project Zero
A
About on SuperTechFans
罗磊的独立博客
WordPress大学
WordPress大学
月光博客
月光博客
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
AI
AI
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
T
Tor Project blog
V
Vulnerabilities – Threatpost
C
Cisco Blogs
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MyScale Blog
MyScale Blog
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
博客园 - 叶小钗
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Simon Willison's Weblog
Simon Willison's Weblog
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic

博客园 - 团团ta爸

SQLSERVER吞噬内存解决记录 新书出炉了,《asp.net4+jQuery 构建信息门户网站》,全程录屏,谢谢支持! HTML+CSS+Javascript教学视频【0409更新】 jQuery递归遍历JSON树,生成对应的ul-li组合,形成树形菜单 解决MSSQL全文检索不支持office2007,2010中docx等格式的问题 不写东西的这几年 4月27日顶尖Windows内核技术大师David A. Solomon与您相约上海 上海.NET俱乐部10月份活动 关于企业软件资质申请流程以及时间规划(二)——软件登记测试 使用Rose2003进行数据库建模并导入SQLServer2000的图解详细过程 关于企业软件资质申请流程以及时间规划(一)——软件著作权申请 写了个小程序,方便大家编程(QuickDog,快捷键帮手) 在VS.NET2005中使用java代码段以及SOL文件格式的解析 C++20周年大庆摘记 使用Hook(钩子)阻止Flash启动浏览器打开URL 2005年8月13日 上海.NET俱乐部第一次活动纪实 已经发布,资料提供下载 你为什么不用Flash做程序的表示层呢? 用于Blog的天气预报服务-改进2005-08-06 工作一周年祭
ASP.NET MVC 自定义路由中几个需要注意的小细节
团团ta爸 · 2014-02-04 · via 博客园 - 团团ta爸

本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节。 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定义格式的路由时,有几点要注意:

1、arg1/arg2/arg3 的部分应该在 routes.MapRoute 中设置默认值UrlParameter.Optional,才允许同时访问只传部分值比如 只传 arg1,或者 arg1/arg2 这样的路径

2、在设置默认值的情况下,如果出现 http://localhost/Home/About/arg1//arg3 这样的链接,到底arg2是否有传值进来?

3、对于http://localhost/Home/ShowAbout/11?arg1=1&arg2=2&arg3=333 这样的链接,到底 arg1取的是1还是11?

以下为路由配置的代码,并没有为test1和test2设置参数默认值

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "ShowAbout",
                url: "Home/ShowAbout/{arg1}/{arg2}/{arg3}",
                defaults: new { controller = "Home", action = "ShowAbout", arg1 = UrlParameter.Optional}
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

 以下为HomeController中的相关代码

public JsonResult ShowAbout(string arg1, string arg2, string arg3) {
            return Json(arg1 + "," + arg2 + "," + arg3, JsonRequestBehavior.AllowGet);
        }

当我们访问链接 http://localhost/Home/ShowAbout/arg1/arg2/arg3 时,会出现如下结果:

但如果少了一个参数,比如访问 http://localhost/Home/ShowAbout/arg1/arg2 ,则会出现 404 的错误

这种情况下,需要在RouteConfig中配置默认值

routes.MapRoute(
                name: "ShowAbout",
                url: "Home/ShowAbout/{arg1}/{arg2}/{arg3}",
                defaults: new {
                    controller = "Home",
                    action = "ShowAbout",
                    arg1 = UrlParameter.Optional,
                    arg2 = UrlParameter.Optional,
                    arg3 = UrlParameter.Optional
                }
            );

UrlParameter.Optional解决了不管参数是值类型还是引用类型,在未传对应参数的情况下,都会提供一个默认值(如0或者null)

这个时候再访问链接 http://localhost/Home/ShowAbout/arg1/arg2  ,则会出现如下结果,而不会报错

当我们传入http://localhost/Home/ShowAbout/arg1//arg3,也就是故意不传arg2的值的时候,会发现结果是

也就是arg3实际传给了参数arg2的位置,两个//还是三个///都会被忽略成一个 /

当我们访问 http://localhost/Home/ShowAbout/11?arg1=1&arg2=2&arg3=333 这样的链接时候,发现结果是:

即当Action方法的参数是Binding类型的时候, MVC框架会将路由参数优先于查询字符串值