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

推荐订阅源

V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Vercel News
Vercel News
C
Check Point Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
T
The Exploit Database - CXSecurity.com
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
P
Proofpoint News Feed
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
IT之家
IT之家
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
Y
Y Combinator Blog
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
T
Tenable Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 最新话题
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
A
Arctic Wolf
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
雷峰网
雷峰网
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence

博客园 - 张剑

WebMatrix&Razor建站系列之WebMatrix介绍 Windows Phone 7 XNA开发之关于游戏组件 Windows Phone 7 XNA开发之关于图形的配置 EntityFramework外健的读写 vs2010中添加项目中找不到EntityFramework实体框架解决办法 使用MSDN学习ASP.NET的工作流程 Windows Phone 7、XNA的旋转的背景 《XNA游戏开发》在战机游戏中使用Decorator模式 不被重视的基础,简单高效地使用ADO.net连接对象 微软2011 GCR MVP Open Day 之旅! ASP.NET4.5与VisualStudio11预览 程序员杂记系列文章,30岁之前的回忆。 程序员杂记:带面具的生活! Windows Phone 7之HelloWorld! MVC3+Entity Framework 实现投票系统(三) MVC3+Entity Framework 实现投票系统(二) MVC3+Entity Framework 实现投票系统(一) 关于Windows Phone 7开发工具离线安装包 程序员杂记:我们的爱情故事
MVC中在路由表routes集合中添加Route实例的一些问题。
张剑 · 2011-11-28 · via 博客园 - 张剑

2011-11-28 20:31  张剑  阅读(545)  评论()    收藏  举报

昨天有位同学问到关于在routes集合中添加自定义的路由实例的问题,她出现的问题是这样的:

1.因为要在应用程序戾动时将实例添加到routes表中,所以需要把代码写在Global类的RegisterRoutes方法中,代码如下两种方式:

  1. routes.Add("s", new Route("a/b", new WebFormRouteHandler("~/WebForm1.aspx")));
  1. routes.Add("a", new Route("c/b", new RouteValueDictionary { { "controller", "Default" }, { "Home", "Index" } }, new WebFormRouteHandler("~/WebForm1.aspx")));

其中,a为路由的名称,Route为路由的实例,同时,用到了一个WebFormRouteHandler的自定义类,该类的作用为处理一个虚拟路径并指定到一个实际的地址,具体代码如下:

  1. public class WebFormRouteHandler : MvcRouteHandler
  2. {
  3. private string VirtualPath;
  4. public WebFormRouteHandler(string path)
  5. {
  6. VirtualPath = path;
  7. }
  8. protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
  9. {
  10. var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
  11. return page;
  12. }
  13. }

她的问题是,用上边第一种方式,那么在做RedirectToAction()操作时,就会直接跳转到WebForm1.aspx页面,而使用第二种方式时,则正常,只有在访问c/b时,才会访问到该页面。

对比上面两段代码,实际上第二种方式只是多了一个关于RouteValueDictionary类的实例,微软官方对这个实例的定义为:

表示不区分大小写的键/值对的集合,您可以在路由框架中的不同位置(例如,在定义路由的默认值时或在生成基于路由的 URL 时)使用该集合。

其实这个对象的创建,可以为url也就是c/b定义了一种默认的访问规则,那么url就一定会按照这种规则去访问指定的页面。