分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

.Net Mvc 4 Route路由

发布时间:2023-09-06 01:56责任编辑:傅花花关键词:暂无标签

1.前言

  在创建一个MVC项目后就可以,在App_Start文件下的RouteConfig.cs里面就可以定义我们的路由规则,其中已经有默认的路由规则了,routes.IgnoreRoute是让路由规则忽略部分文件,如下:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute( ???name: "Default", ???url: "controller}/{action}/{id}", ???defaults: new { controller = "Home", action = "Index", id = ????UrlParameter.Optional });

  在此处定义的路由规则后还需要在Global.asax文件下注册路由规则,之后url访问就会自上而下的去匹配路由规则,匹配到就不再往下匹配了,所以要把特殊的路由规则放在前面,最后才是通用规则。

RouteConfig.RegisterRoutes(RouteTable.Routes);

  MapRoute有以下五个静态重载方法,参数含义分别是:routes:应用程序的路由的集合(这里是不用填的,因为this这个关键字)。name:要映射的路由的名称。url:路由的 URL 模式。defaults:一个包含默认路由值的对象。namespaces:应用程序的命名空间constraints:路由约束,用于指定 url 参数的值。接下来展示这五个重载方法使用的例子,每个例子讲他们的重点参数,重复的参数不累赘描述。

public static Route MapRoute(this RouteCollection routes, string name, string url);public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults);public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces);public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints);public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces);public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces);

 2.MapRoute(this RouteCollection routes, string name, string url)

  这个方法有两个参数,一个是name:这个路由规则的名称,url:路由url定义。name可以随便取名,url后的{controller}和{action}是一定要这么命名的并且成对出现缺一不可,这是规定。这个方法由于没有默认的参数,所以当url为http://localhost:65398/的时候会404.

routes.MapRoute( ???name: "Method1", ???url: "{controller}/{action}/{id}");

  URL定义如何那么匹配的url就是怎么样的,多一个参数也会无法匹配,所以当有多个参数的时候就可以加入{*catchall}捕获多个参数,也可以在前面加上前缀,如下所示。

routes.MapRoute( ???name: "Method1", ???url: "Homes/{controller}/{action}/{*catchall}");

  如果我们要把MVC设置伪静态的话就可以如下所示,不过这样也会因为被认为是静态文件而被跳过显示404,我们可以在web.config文件中添加如下的代码。

routes.MapRoute( ???name: "Method1", ???url: "{controller}/{action}.html");
<system.webServer> ???<handlers> ???????<add name="ReWriteHtmlHandler" path="*.html" modules="IsapiModule" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> ???</handlers></system.webServer> ???

3. MapRoute(this RouteCollection routes, string name, string url, object defaults)

  defaults:就是默认路由。如果URL是http://localhost:65398/这样的情况,就会进入这个默认路由了。如果url参数中有{id}的定义就必须在defaults参数中定义id=UrlParameter.Optional,原因在这篇博客中有解释https://www.cnblogs.com/pipizhu/archive/2011/08/10/2134222.html

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

4.MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints)

  constraints:条件约束,可以在这里加入正则表达式来约束controller和acion或者是请求方式get or post,只有满足这个约束的路径才会被匹配,不满足就不匹配。

routes.MapRoute( ???name: "Default", ???url: "{controller}/{action}", ???defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, ???constraints: new { controller = "^H.*", action = "^Index$|^About$", httpMethod = new HttpMethodConstraint("GET") });

  也可以直接强制要求访问指定的controller或者action。

routes.MapRoute( ???name: "Default", ???url: "{controller}/{action}", ???defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, ???constraints: new { controller = "Homes", action = "index", httpMethod = new HttpMethodConstraint("GET") });

5.MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces)

  namespaces:命名空间。优先先访问在这个命名空间下的controller和action,匹配到就结束匹配,如果没有找到就继续执行到别的命名空间匹配,可以设置多个命名空间。

routes.MapRoute( ???name: "Default", ???url: "{controller}/{action}", ???defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, ???namespaces: new[] { "ZNM.WEB.Controllers", "ZNM.WEB.Controllers.Admin.Znm" });

6.MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)

  这个方法就是把前面的参数全都混合起来用啦,这里不多复述。

7.总结

  路由的基础使用方法就是以上几种,基本上通常的默认的就够用了。可能我的理解也有问题,请大家指出。

.Net Mvc 4 Route路由

原文地址:https://www.cnblogs.com/xwc1996/p/9079444.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved