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

推荐订阅源

The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
O
OpenAI News
NISL@THU
NISL@THU
AI
AI
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
W
WeLiveSecurity
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
D
Docker
博客园 - 三生石上(FineUI控件)
T
Troy Hunt's Blog
T
Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
H
Hacker News: Front Page
B
Blog
S
SegmentFault 最新的问题
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
小众软件
小众软件
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
PCI Perspectives
PCI Perspectives
I
Intezer
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed

博客园 - YbSoftwareFactory

YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法 YbSoftwareFactory 代码生成插件【二十三】:集成强大的公文流转系统 YbSoftwareFactory 代码生成插件【二十二】:CMS基础功能的实现 YbSoftwareFactory 代码生成插件【二十一】:Web Api及MVC性能提升的几个小技巧 YbSoftwareFactory 代码生成插件【二十】:DynamicObject的序列化 YbSoftwareFactory 代码生成插件【十九】:实体类配合数据库表字段进行属性扩展的小技巧 YbSoftwareFactory 代码生成插件【十八】:树形结构下的查询排序的数据库设计 YbSoftwareFactory 代码生成插件【十七】:先进的权限模型体系设计 YbSoftwareFactory 代码生成插件【十六】:Web 下灵活、强大的审批流程实现(含流程控制组件、流程设计器和表单设计器) YbSoftwareFactory 代码生成插件【十五】:Show 一下最新的动态属性扩展功能与键值生成器功能 YbSoftwareFactory 代码生成插件【十四】:通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页 YbSoftwareFactory 代码生成插件【十三】:Web API 的安全性 YbSoftwareFactory 代码生成插件【十二】:超级灵活方便的应用程序设置管理API YbSoftwareFactory 代码生成插件【十一】:ASP.NET WebApi MVC下组织机构管理和菜单权限管理的实现 YbSoftwareFactory 代码生成插件【十】:ASP.NET WebApi MVC下审计、缓存和导出功能的实现 YbSoftwareFactory 代码生成插件【九】:基于JQuery、WebApi的ASP.NET MVC插件的代码生成项目主要技术解析 YbSoftwareFactory 代码生成插件【八】:基于JQuery EasyUI、Web Api的 ASP.NET MVC 代码生成插件 YbSoftwareFactory 代码生成插件【七】:YbRapidSolution for WinForm 插件生成项目总体架构介绍 YbSoftwareFactory 代码生成插件【六】:WinForm 完美解决方案的代码生成插件
YbSoftwareFactory 代码生成插件【二十四】:MVC中实现动态自定义路由
YbSoftwareFactory · 2016-03-08 · via 博客园 - YbSoftwareFactory

   上一篇介绍了 公文流转系统 的实现,本篇介绍下MVC下动态自定义路由的实现。

   在典型的CMS系统中,通常需要为某个栏目指定个友链地址,通过指定友链地址,该栏目的地址更人性化、方便记忆,也有利用于搜索引擎优化。

   但在MVC中,通常需要在应用程序启动时注册路由规则,该路由规则又通常和控制器进行了关联,也就是某个地址通常情况下都是有对应的控制器进行处理的。但在MVC中如何做到自定义动态路由,以便能在运行时通过某个控制器处理一些运行时动态设定的Url地址呢?

   方法当然是有的:

   1、首先实现一个动态路由约束条件类:

   该类用于判断指定的路由地址是否满足特定的路由规则,当然该规则需要在后面进行注册才会生效。

 1 public class CmsUrlConstraint : IRouteConstraint
 2     {
 3         public CmsUrlConstraint()
 4         {
 5             
 6         }
 7         /// <summary>
 8         /// 动态查询数据库,判断当前请求地址是否满足约定
 9         /// </summary>
10         /// <param name="httpContext"></param>
11         /// <param name="route"></param>
12         /// <param name="parameterName"></param>
13         /// <param name="values"></param>
14         /// <param name="routeDirection"></param>
15         /// <returns></returns>
16         public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
17             RouteDirection routeDirection)
18         {
19             if (values[parameterName] != null)
20             {
21                 //此为请求的友链地址参数
22                 var permalink = values[parameterName].ToString();
23 
24                 var dbcontext = new YbObjectContext("YbRapidSolution");
25                 //根据友链地址参数,查找栏目,存在则表示匹配成功
26                 var page = dbcontext.Set<CmsColumn>()
27                     .Where(p => p.Permalink == permalink).FirstOrDefault();
28                 if (page != null)
29                 {
30                     //匹配成功进行数据传值
31                     HttpContext.Current.Items["cmspage"] = page;
32                     return true;
33                 }
34                 return false;
35             }
36             return false;
37         }
38     }

   2、然后注册路由CmsColumnRoute和CmsRoute,指定上述满足约束条件的地址该由哪个真实的控制器进行处理:

 1 public static void RegisterRoutes(RouteCollection routes)
 2         {
 3             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 4             //routes.IgnoreRoute("{UEditor/controller}.ashx{*pathInfo}");
 5             routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
 6 
 7             routes.MapRoute(
 8             name: "CmsColumnRoute",
 9             url: "{*permalink}",
10             defaults: new { controller = "Home", action = "ColumnPage", id = UrlParameter.Optional },
11             constraints: new { permalink = new CmsUrlConstraint() }
12             );
13             routes.MapRoute(
14             name: "CmsRoute",
15             url: "{action}/{*permalink}",
16             defaults: new { controller = "Home", action = "ColumnPage", id = UrlParameter.Optional },
17             constraints: new { permalink = new CmsUrlConstraint() }
18             );
19 
20             routes.MapRoute(
21                 name: "Default",
22                 url: "{controller}/{action}/{id}",
23                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
24             );
25         }

   运行原理:

   1、输入特定地址

   2、系统通过CmsUrlConstraint类的Match方法查询数据库,如该地址和某个栏目中设定的友链地址一致,则表示匹配成功

   3、如匹配成功,则通过注册路由时设定的Controller和Action进行后续处理并返回View。

   具体过程如下:

   实现效果如下:

   1、运行过程中可随意指定该栏目的地址:

   2、然后通过指定的地址可访问该页面,但我们并没有专门实现News的控制器: