MVC – 15.路由机制
15.1.路由检测插件 - RouteDebug
15.2.路由约束
15.3.命名路由
15.4.验证码
15.5.ASP.NET MVC 与 三层架构
15.6.Area区域
- 15.6.1.尝试将区域注册类放到 另外一个程序集中
- 15.6.2.AreaName
- 15.6.3.将区域控制器放到外面程序集
15.7.将控制器类都放到单独程序集中
15.8.重新使用外部项目保存区域控制器
15.1.路由检测插件 - RouteDebug
具体参阅 官方网站。
下载地址:https://files.cnblogs.com/tangge/RouteDebugger-2.1.3.0.zip
RouteDebugger的使用方法和RouteDebug的差不多,都是新建一个lib文件放第三方插件,然后添加引用。MVC3的不需要在Global文件里面的Application_Start中注册,这是因为.NET4.0新增的程序集Microsoft.Web.Infrastructure允许动态注册HttpModule,RouteDebugger将格式化的路由调试信息追加(append)到每一个request里。这里需要注意一下,如果web.config文件中没有如下代码的要记得添加上
<appSettings> <add key="RouteDebugger:Enabled" value="true"/> </appSettings>
15.2.路由约束
允许URL段使用正则表达式来限制路由是否匹配请求
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Stu", action = "Index", id = UrlParameter.Optional } , constraints: new { id = @"\d" } ); routes.MapRoute( name: "Default2", url: "{controller}/{action}/{name}", defaults: new { controller = "html", action = "printname", name = UrlParameter.Optional } , constraints: new { name = "[a-z]+" } );
HtmlController.cs
// html/printid/1 public ActionResult PrintId(int? id) { if (id == null) { id = 0; } return Content("id=" + id.ToString()); } // html/printname/mama public ActionResult PrintName(string name) { if (name == null) { name = ""; } return Content("name=" + name.ToString()); }
15.3.命名路由
生成指定路由名的 url超链接
@Html.RouteLink("test", "Default",
new {contorller="home",action ="index",id=1 } );
会按照找到的路由规则生成超链接(没有使用默认值)
15.4.验证码
创建一个控制器 HelpController.cs
public class HelpController : Controller { /// <summary> /// 验证码 /// </summary> /// <returns></returns> public ActionResult VCode() { Common.VCode v = new Common.VCode(); byte[] arrImg = v.GetVCode(); return File(arrImg, "image/jpeg"); } }
引用一个生成验证码图片 字节数组
VCode.cs文件
VCode.cs
VCode.cs using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; using System.IO; namespace _06MVCAjax_CodeFirst.Common { public class VCode { /// <summary> /// 生成验证码图片 字节数组 /// </summary> /// <returns></returns> public byte[] GetVCode() { using (Image img = new Bitmap(65, 30)) { string strCode = GetRandomStr(); HttpContext.Current.Session["vcode"] = strCode; using (Graphics g = Graphics.FromImage(img)) { g.Clear(Color.White); g.DrawRectangle(Pens.Blue, 0, 0, img.Width - 1, img.Height - 1); DrawPoint(g); g.DrawString(strCode, new Font("微软雅黑", 15), Brushes.Blue, new PointF(5, 2)); DrawPoint(g); using (System.IO.MemoryStream ms = new MemoryStream()) { //将图片 保存到内存流中 img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //将内存流 里的 数据 转成 byte 数组 返回 return ms.ToArray(); } } } return null; } Random random = new Random(); string GetRandomStr() { string str = string.Empty; string[] strArr = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }; for (int i = 0; i < 4; i++) { int index = random.Next(strArr.Length); str += strArr[index]; } return str; } void DrawPoint(Graphics g) { Pen[] pens = { Pens.Blue, Pens.Black, Pens.Red, Pens.Green }; Point p1; Point p2; int length = 1; for (int i = 0; i < 50; i++) { p1 = new Point(random.Next(79), random.Next(29)); p2 = new Point(p1.X - length, p1.Y - length); length = random.Next(4); g.DrawLine(pens[random.Next(pens.Length)], p1, p2); } } } }
15.5.ASP.NET MVC 与 三层架构
MVC是表现模式(设计模式)
三层是架构模式(代码架构/物理架构)
15.6.Area区域
MVC 项目目录结构 缺点:
1.不利于分功能协作开发(购物车/商品管理/用户权限管理…)
2.代码结构臃肿…
15.6.1.尝试将区域注册类放到 另外一个程序集中
查找方式
1.程序集
2.命名空间
3.是否继承AreaRegistration
15.6.2.AreaName
public class SbAreaRegistration : AreaRegistration
{
public override string AreaName //AreaName在某个区域的 控制器的Action方法 加载视图时 作为 路径的一部分使用
{
get
{
return "Sb3";
}
}
15.6.3.将区域控制器放到外面程序集
15.7.将控制器类都放到单独程序集中
依赖注入:当前类里面有一个全局变量,这个变量在类里面不实例化,由外面通过属性或者构造函数传进来,这个传的过程就叫依赖注入。
例:
FangZi类,声明一个BingXiang对象。BingXiang到底是5000还是50000,FangZi决定不了。
FangZi.属性,从外面new 一个BingXiang传进来,这个过程就是依赖注入。
FangZi.BingXiang = xxx;
反过来,FangZi里面没有BingXiang。外面new一个BingXing传进来,这个就叫控制反转。
(将控制器类都放到单独程序集中)步骤:
其实就是看是否继承自 System.Web.Mvc 里面的 Controller 类
加命名空间执行的结果是一样的,但是提高效率·自定义控制访问。(多命名空间的时候)
15.8.重新使用外部项目保存区域控制器
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/p/3955516.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具