zoukankan      html  css  js  c++  java
  • 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 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  
    诚邀各路高手、初学者加入。

     
     
     
    标签: ASP.NET MVC
  • 相关阅读:
    生活希望[置顶] 程序员的奋斗史(十六)——谈习惯
    评论数据库Win A Free Copy of Packt’s Managing Multimedia and Unstructured Data in the Oracle Database ebook
    下载方法[jBPM5 部署] 最可靠,最简洁方式
    美感地方Python禅
    关系方向机缘之始
    截止账户[置顶] 国际黄金强势上涨投资者加油~~~~~~
    数据库书籍大师推荐的Oracle数据库相关的书籍,收集汇总。
    输出空格杨辉三角 (a+b)的n次幂的展开式中各项的系数很有规律,对于n=2,3,4时分别是:1 2 1, 1 3 3 1,1 4 6 4 1。这些系数构成了著名的杨辉三角形:
    错误删除Its not about the outage
    noncompatible bean definition of same name and class
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3873963.html
Copyright © 2011-2022 走看看