当你创建一个新的asp.net mvc应用程序时,应用程序已经被配置为使用asp.net路由.asp.net路由在两个地方设置。
第一点,在你的应用程序的web配置文件(web.config文件文件)中启用asp.net路由在配置文件中有四个节点与路由有关:sytem.web.httpmodules节,system.web.httphandlers节,system.webserver.modules节,以及system.webserver.handlers节。特别要小心不要删除了这些节点,因为没有它们路由将不能工作。
第二点,也是更为重要的一点,在应用程序的global.asax中文件中创建了一个路由表.global.asax文件是一个特殊的文件,它包含了作用于asp.net应用程序生命周期事件的事件处理程序。路由表在应用程序开始事件期间创建。
代码清单1中的文件包含了一个asp.net mvc应用程序的默认global.asax文件。
代码清单1 - global.asax.cs
使用系统; 使用 system.collections.generic; 使用 system.linq; 使用 system.web; 使用 system.web.mvc; 使用 system.web.routing; 命名空间 mvcapplication1 { //注意:有关启用iis6或iis7经典模式的说明, 请访问http://go.microsoft.com/?linkid=9394801 public class mvcapplication:system.web.httpapplication { public static void registerroutes(routecollection路由) { routes.ignoreroute( “{resource} .axd / {* pathinfo}”); 路线。 maproute( “default”, // route name “{controller} / {action} / {id}”, //具有参数 的url new {controller = “home”,action =“index”,id =“” } //参数默认值 ); } protected void application_start() { registerroutes(routetable.routes); } } } //具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){registerroutes(routetable.routes); }}} //具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){registerroutes(routetable.routes); }}} } protected void application_start(){registerroutes(routetable.routes); }}} } protected void application_start(){registerroutes(routetable.routes); }}}
当一个mvc应用程序首次运行时,会调用的application_start()方法。这个方法随后调用了的registerroutes()方法.registerroutes()方法创建了路由表。
默认的路由表包含了一个路由(名叫默认).default路由将url的第一部分映射到控制器名,url的第二部分映射到控制器动作,第三个部分映射到一个叫做id的参数。
假设你在浏览器的地址栏输入了下面的网址:
/首页/索引/ 3
默认的路由将这个url映射为下面的参数:
控制器=首页
action = index
id = 3
当你请求url /首页/索引/ 3时,将会执行下面的代码:
homecontroller.index(3)
默认路由包含了所有三个参数的默认值。如果你不提供控制器,那么控制器参数默认值为首页。如果你不提供动作,动作参数默认为值指标。最后,如果你不提供id,id参数默认为空字符串。
让我们看看几个例子,默认路由是如何将url映射到控制器动作的设想你在浏览器地址栏输入了下面的网址:
/家
由于缺省路由参数的默认值,输入这个网址将会调用代码清单2中的homecontroller的类的指数()方法。
代码清单2 - homecontroller.cs
使用 system.web.mvc; 命名空间 mvcapplication1.controllers { [handleerror] public class homecontroller:controller { public actionresult index(string id) { return view(); } } }
在代码清单2中,homecontroller类包含了一个叫做index()的方法,它接受一个叫做id的参数.url / home将会导致调用index()方法,并使空字符串作为id参数的值。
出于mvc框架调用控制器动作的方式,url / home也匹配代码清单3中homecontroller类的索引()方法。
代码清单3 - homecontroller.cs(不含参数的索引动作)
使用 system.web.mvc; 命名空间 mvcapplication1.controllers { [handleerror] public class homecontroller:controller { public actionresult index() { return view(); } } }
代码清单3中的index()方法不接受任何的参数.url / home将会导致调用这个index()方法.url / home / index / 3也会调用这个方法(id被忽略)。
url / home也会匹配代码清单4中homecontroller类的索引()方法。
代码清单4 - homecontroller.cs(使用可空参数的索引动作)
使用 system.web.mvc; 命名空间 mvcapplication1.controllers { [handleerror] public class homecontroller:controller { public actionresult index(int?id) { return view(); } } }
在代码清单4中,索引()方法拥有一个整数参数。因为这个参数是一个可空参数(可以拥有空值),因此可以调用指数()而不会引发错误。
最后,使用url / home调用代码清单5中的index()方法将会引发一个异常,因为id参数并非一个可空参数。如果你试图调用index()方法,那么你将会获得一个图1中所示的错误。
代码清单5 - homecontroller.cs(含有id参数的索引动作)
使用 system.web.mvc; 命名空间 mvcapplication1.controllers { [handleerror] public class homecontroller:controller { public actionresult index(int id) { return view(); } } }
图01:调用一个期望参数值的控制器动作
另一方面,url / home / index / 3能够与代码清单5中的索引控制器动作很好地工作./home/index/3请求将会引发调用含有一个id的索引()方法,且该id值为3。
这篇教程的目的是为你提供一个asp.net路由的简短介绍。我们仔细查看了默认的路由表,它在你创建新的asp.net mvc应用程序时获得。你学习了默认的路由表如何将url映射到控制器动作。
2.创建自定义路由
在这篇教程中,你会学习到如何为asp.net mvc应用程序添加自定义路由。你会学习如何将global.asax文件中的默认路由表修改为自定义路由。
对于简单的asp.net mvc应用程序,默认的路由表已经可以很好的完成工作了。然而,你可以发现会存在特定的路由需求在这种情况下,你可以创建一个自定义路由。
设想一下,举个例子,你正在创建一个博客应用程序你可能想要像这样处理即将到来的请求:
/存档/ 2009年12月25日
当用户输入这一请求,你想要返回对应于日期2009年12月25日的博客条目。为了处理这种类型的请求,你需要创建一个自定义路由。
代码清单1中的global.asax包含了一个新的自定义路由,命名为了博客,它处理了类似/存档/条目日期这样的请求。
代码清单1 - global.asax(含有自定义路由)
使用 system.web.mvc; 使用 system.web.routing; 命名空间 mvcapplication1 { public class mvcapplication:system.web.httpapplication { public static void registerroutes(routecollection routes) { routes.ignoreroute(“{resource} .axd / {* pathinfo}”); routes.maproute( “blog”, //路由名称 “archive / {entrydate}”, //具有参数 的url new {controller = “archive”,action =“ } //参数defaults ); routes.maproute( “default”,//路由名称 “{controller} / {action} / {id}”,//具有参数 的url new {controller = “home”,action =“index”,id =“” } //参数defaults ); } protected void application_start() { registerroutes(routetable.routes); } } } } //参数defaults); routes.maproute(“default”,//路由名称“{controller} / {action} / {id}”,//具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){ registerroutes(routetable.routes); } } } ,//路由名称“{controller} / {action} / {id}”,//具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){ registerroutes(routetable.routes); } } } , //路由名称“{controller} / {action} / {id}”, //具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){ registerroutes(routetable.routes); } } } //具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){ registerroutes(routetable.routes); } } } //具有参数的url new {controller = “home”,action =“index”,id =“” } //参数defaults); } protected void application_start(){ registerroutes(routetable.routes); } } }
添加到路由表中的路由顺序非常重要。我们的新自定义博客路由在现有的默认路由前面。如果你将这个顺序颠倒过来,那么默认路由将总是被调用,而不是自定义路由。
自定义博客路由匹配任何以/存档/作为开始的请求因此,它匹配所有下面的网址:
/存档/ 2009年12月25日
/存档/ 2004年10月6日
/存档/苹果
自定义路由将即将到来的请求映射到名为存档的控制器,并且调用了条目()动作。当调用项()方法时,条目日期作为entrydate参数进行了传递。
代码清单2 - archivecontroller.cs
使用系统; 使用 system.web.mvc; 命名空间 mvcapplication1.controllers { public class archivecontroller:controller { public string entry(datetime entrydate) { return “您从” + entrydate.tostring())请求了条目。 } } }
注意到代码清单2中的条目()方法接受一个日期时间类型的参数.mvc框架非常的聪明,足以自动地将url中的条目日期转换为日期时间值。如果url中的条目日期参数不能转换为日期时间,将会引发错误(如图1)。
图1 - 转换参数时的错误
这篇教程的目的是演示如何创建自定义路由。你学习了如何在global.asax中文件的路由表中添加自定义路由,该路由代表着博客条目。我们讨论了如何将对博客条目的请求映射到名为archivecontroller的控制器,和名为项()的控制器动作上。
3.创建路由约束
你可以使用路由约束来限制匹配特定路由的浏览器请求。可以使用正则表达式来指定一个路由约束。
举个例子,假设你已经在global.asax中文件中定义了一个路由。
代码清单1 - global.asax.cs
routes.maproute( “product”, “product / {productid}”, new {controller =“product”,action =“details”} );
代码清单1包含一个叫做产品的路由。你可以使用产品路由将浏览器请求映射到代码清单2中的productcontroller的。
代码清单2 - controllers \ productcontroller.cs
使用 system.web.mvc; 命名空间 mvcapplication1.controllers { public class productcontroller:controller { public actionresult details(int productid) { return view(); } } }
注意到产品控制器公布的详细信息()动作接受一个叫做的productid的参数。这个参数是一个整数参数。
定义在代码清单1中的路由将会匹配下面的任意网址:
/产品/ 23 /产品/ 7 不幸的是,路由也会匹配下面的网址: /产品/嗒嗒 /产品/苹果
因为详细()动作期望的是一个整数值,发起一个含有非整数值的请求将会导致错误。举个例子,如果你在浏览器中输入/产品/苹果网址,那么你将会得到图1所示的错误页。
图1:错误页
你实际想做的是只匹配包含合适整数productid参数的url。当定义路由来限制与路由相匹配的网址时,你可以使用约束。代码3中的修改后的产品路由包含了一个正则表达式,它限制了只匹配数字。
代码清单3 - global.asax.cs
routes.maproute( “product”, “product / {productid}”, new {controller =“product”,action =“details”}, new {productid = @“\ d +”} )
正则表达式\ d +匹配一个或多个整数这个限制使得产品路由匹配了下面的网址:
/产品/ 3 /产品/ 8999 但是不匹配下面的网址: /产品/苹果 /产品
这些浏览器请求将自由另外的路由处理,或者,如果没有匹配的路由,将会返回一个“资源找不到”错误。
创建一个自定义路由约束
这篇教程的目标是演示如何创建一个自定义路由约束。自定义路由约束允许你阻止某个路径被匹配,除非满足一些自定义的条件。
在这篇教程中,我们创建了一个本地主机路由约束.localhost路由约束只匹配本地计算机发出的请求。通过互联网发出的远程请求不会被匹配。
。你可以通过实现irouteconstraint接口来实现一个自定义路由这是一个极其简单的接口,它只描述了一个方法:
bool match( httpcontextbase httpcontext, route route, string parametername, routevaluedictionary值, routedirection routedirection )
这个方法返回一个布尔值。如果返回了假,与约束相关联的路由将不会匹配浏览器请求。
本地主机约束包含在了代码清单1中。
代码清单1 - localhostconstraint.cs
使用 system.web; 使用 system.web.routing; 命名空间 mvcapplication1.constraints { public class localhostconstraint:irouteconstraint { public bool match ( httpcontextbase httpcontext, route route, string parametername, routevaluedictionary values, routedirection routedirection ) { return httpcontext.request.islocal; } } }
代码清单1中的约束利用了httprequest的类公布的islocal属性。当发出请求的ip地址是127.0.0.1或者与服务器的ip地址相同时,这个属性返回真。
你在定义于global.asax中的路由中使用了自定义约束。代码清单2中的global.asax中文件使用了本地主机约束来阻止任何人请求管理员页面,除非他们从本地服务器发出请求。举个例子,当请求来自远程服务器时,对于/管理/ deleteall的请求将会失败。
代码清单2 - global.asax
使用系统; 使用 system.collections.generic; 使用 system.linq; 使用 system.web; 使用 system.web.mvc; 使用 system.web.routing; 使用 mvcapplication1.constraints; 命名空间 mvcapplication1 { public class mvcapplication:system.web.httpapplication { public static void registerroutes(routecollection routes){ routes.ignoreroute( “{resource} .axd / {* pathinfo}”); routes.maproute( “admin”, “admin / {action}”, islocal = new localhostconstraint()} ); //routes.maproute( // “默认”,//路线名称 // “{控制器} / {行动} /(编号)”,// url与参数 // 新 {控制器= “主页”,动作=“索引“,id =”“ } //参数defaults //); } protected void application_start(){ registerroutes(routetable.routes); } } }
本地主机约束使用在了管理路由的定义中这个路由不会被远程浏览器请求所匹配然而,应该意识到定义在global.asax中中的其他路由可能会匹配相同的请求理解这一点很重要。:约束阻止了特定路由匹配某一请求,而不是所有定义在global.asax中文件中的路由。
注意到默认路由在代码清单2中的glabal.asax文件中被注释掉了。如果你包含默认路由,那么默认路由将会匹配对管理控制器的请求。在这种情况下,远程用户仍然可以调用管理控制器的动作,即使他们的请求不匹配管理路由。
【相关推荐】
1. 什么是asp.net mvc ?总结asp.net mvc
2. 详细介绍asp.net mvc--控制器(controller)
3. 详细介绍asp.net mvc--视图
4. 深入了解asp.net mvc与webform的区别
5. 通过asp.net mvc开发微信自定义菜单编辑工具的代码示例
以上就是详细介绍asp.net mvc--路由的详细内容。
