打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ASP.NET MVC 及 Areas 简单控制路由
ASP.NET MVC 及 Areas 简单控制路由

ASP.NET MVC中怎么去控制路由,这个想关的文章很多,我在这里就是自我总结一下,仅供参考。

1.我们新建一个项目,查看RouteConfig.cs,代码如下:

 1 public static void RegisterRoutes(RouteCollection routes) 2         { 3             routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 4  5             routes.MapRoute( 6                 name: "Default", 7                 url: "{controller}/{action}/{id}", 8                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 9             );10         }

第3行,routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 表示忽略有扩展名  axd的路由,

你可以仿照它,如果你项目里有哪些文件是不攻外部访问的,可以全部过滤掉。

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

这个就是最基本的路由控制了。表示首页是HomeController里的ActionResult Index,默认路由参数为id

下面我们来改改这串代码,如下:

1 ///首页2             routes.MapRoute(3                 "Index", // Route name4                 "{controller}/{action}",5                 new { controller = "Index", action = "Index", id = UrlParameter.Optional },6                 new string[] { "SnsCWan.Controllers" }7             );

有备注大伙应该看得懂,其中 new string[] { "SnsCWan.Controllers" } 里的SnsCWan是当前网站名称,这个先注意一下。

1 ///PayStepIndex2             routes.MapRoute(3                 "PayStepIndex", // Route name4                 "{controller}/{action}/{Method}/{id}.html",5                 new { controller = "PayStep", action = "Index", Method = UrlParameter.Optional, id = UrlParameter.Optional },6                 new string[] { "SnsCWan.Controllers" }7             );

这个是有控制多个参数的路由,你可以改 "{controller}/{action}/{Method}/{id}.html" 来设置你需要的路由方式。

 

下面我们新建一个Admin的Areas,表示站点的管理员部分,默认生成的代码如下:

 

1 public override void RegisterArea(AreaRegistrationContext context)2         {3             context.MapRoute(4                 "Admin_default",5                 "Admin/{controller}/{action}/{id}",6                 new { action = "Index", id = UrlParameter.Optional }7             );8         }

 

同一个网站里面,如果根目录下的路由和Areas下的路由同时都路由到一个Index/Index,此时项目就会报错,告诉你同时存在了2个路由,这个时候怎么处理呢? 如下:

1 context.MapRoute(2                 "Admin_default",3                 "Admin/{controller}/{action}/{id}",4                 new { controller = "Index", action = "Index", id = UrlParameter.Optional },5                 new string[] { "SnsCWan.Areas.Admin.Controllers" }6             );

特别主意 new string[] { "SnsCWan.Areas.Admin.Controllers" } ,这个和根目录下的路由的区别,它表示指定 SnsCWan网站下的Areas区域的Admin控制器,

这样,路由间就不会再产生冲突了。

总得配置路由就是这些,具体怎么去设置更加美观的url还有待你更具创意的想法。

 

本群提供ASP.NET MVC,EF,LINQ,WEB API技术支持,不在乎人多,在乎人精。
ASP.NET MVC群 171560784  
诚邀各路高手、初学者加入。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用Areas分离ASP.NET MVC项目
MVC4 中使用 Area 和 注意的地方
Asp.Net MVC2.0 Url 路由
ASP.NET MVC架构与实战系列之二:理解MVC路由配置
网站后台登录地址大全
Asp.net core 3.1实现路由Url根据命名空间自动生成
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服