zoukankan      html  css  js  c++  java
  • 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 name
    4                 "{controller}/{action}",
    5                 new { controller = "Index", action = "Index", id = UrlParameter.Optional },
    6                 new string[] { "SnsCWan.Controllers" }
    7             );

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

    1 ///PayStepIndex
    2             routes.MapRoute(
    3                 "PayStepIndex", // Route name
    4                 "{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  
    诚邀各路高手、初学者加入。

  • 相关阅读:
    有一天人人都会变成程序猿
    mysql 假设存在id则设数据自添加1 ,不存在则加入。java月份计算比較
    做程序员的老婆应该注意的一些事情
    人类科技的发展为什么会是加速度的(TRIZ方法再推荐)
    Unity5.0 RPG角色扮演历险类游戏之 森林历险记
    linux目录对照命令——meld
    iOS --- [持续更新中] iOS移动开发中的优质资源
    【spring bean】spring中bean的懒加载和depends-on属性设置
    【spring bean】 spring中bean之间的引用以及内部bean
    【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
  • 原文地址:https://www.cnblogs.com/kezhiping/p/3872552.html
Copyright © 2011-2022 走看看