路由是指向一个控制器的http请求的过程,这个处理的功能是在system.web.routing中实现的。 这个程序集不是asp.net mvc的一部分。 它实际上是asp.net运行时的一部分,并且作为.net 3.5 sp1正式发布。
system.web.routing由mvc框架使用,但也被asp.net动态数据使用。 mvc框架利用路由将请求引导至控制器。global.asax文件是应用程序的一部分,可在其中定义应用程序的路由。
这是在上一章创建的mvc应用程序(firstmvcapp)中的global.asax中的应用程序启动事件的代码。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.routing; namespace firstmvcapp { public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); routeconfig.registerroutes(routetable.routes); } } }
以下是routeconfig类的实现,其中包含一个registerroutes方法。
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.routing; namespace firstmvcapp { public class routeconfig { public static void registerroutes(routecollection routes){ routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new{ controller = "home", action = "index", id = urlparameter.optional}); } } }
您将定义路由,这些路由将url映射到特定的控制器操作。一个动作只是控制器上的一个方法。它也可以从url中选择参数并将它们作为参数传递给方法。
所以在应用程序中定义的这个路由是默认路由。从上面的代码可以看出,当你看到一个url以(something)/(something)/(something)的形式到达的时候,第一个是控制器的名称,第二个是动作的名字,第三个是一个id参数。
mvc应用程序使用asp.net路由系统,它决定url如何映射到控制器和操作。
当visual studio创建mvc项目时,它会添加一些默认路由来启动。 运行应用程序时,您将看到visual studio已将浏览器定向到端口56922。几乎可以肯定地每次在浏览器请求的url中看到不同的端口号,因为visual studio在创建项目时会分配一个随机端口。
在最后一个例子中,我们添加了一个控制器:homecontroller ,所以也可以请求任何下面的url,它们将被定向到homecontroller上的index 操作。如下都是有效的url -
http://localhost:56922/home/ -- 或者 -- http://localhost:56922/home/index
当浏览器请求http://mysite/或http://mysite/home时,它会从homecontroller的index方法获取输出。
也可以通过更改浏览器中的url来尝试此操作。在这个例子中,它是http://localhost:56922/,除了端口可能不同。
如果将/home或/home/index追加到url并按下“enter”按钮,将看到与mvc应用程序输出相同的结果。
正如在这种情况下可以看到有一个名为homecontroller的控制器,这个homecontroller将成为mvc应用程序的起点。
visual studio为新项目创建的默认路由。但是如果想遵循自己的约定,那么将需要修改路由。
当然可以添加您自己的路由。如果不喜欢这些动作名称,或有不同的id参数,又或者一般情况下网站的url结构不同,则可以添加自己的路由项。
下面来看一个简单的例子。考虑有一个包含进程列表的页面,以下是将转到流程页面的代码。
routes.maproute( "process", "process/{action}/{id}", defaults: new{ controller = "process", action = "list ", id = urlparameter.optional} );
当客户端请求进来并寻找具有process/action/id的url时,它们将会进入到process控制器。这里可以使动作有点不太一样,默认动作使用的是list而不是index。
现在到达的请求看起来像localhosts/process。 路由引擎将使用这个路由配置来传递它,所以它将使用list作为的默认行为。
以下是完整的类实现。创建一个名称为:myrouteconfig 的类,参考代码 -
using system; using system.collections.generic; using system.linq; using system.web; using system.web.routing; using system.web; using system.web.mvc; using system.web.routing; namespace firstmvcapp { public class myrouteconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "process", "process/{action}/{id}", defaults: new { controller = "process", action = "list", id = urlparameter.optional }); routes.maproute( name: "home", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional }); } } }
第1步 - 运行它并请求一个流程页面,使用以下url :http://localhost:63664/process,应该会看到以下结果 -
将看到一个http 404,因为路由引擎正在查找processcontroller,但是不可用。
第2步 - 通过右键单击解决方案管理器 中的controllers文件夹来创建processcontroller,然后选择:添加 -> 控制器 。
它将显示“添加基架” 对话框。如下图所示 -
第3步 - 选择mvc 5控制器 - 空选项,然后单击“添加” 按钮。添加控制器对话框将出现。
第4步 - 将名称设置为processcontroller,然后单击“添加” 按钮。如下图所示 -
现在,将在controllers文件夹中看到一个新的 c# 文件:processcontroller.cs,在visual studio中打开并进行编辑。如下图所示 -
第5步 - 将返回类型从actionresult更改为string,并使用以下代码从此操作方法返回一些字符串。修改后的 processcontroller 文件中的代码如下 -
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace firstmvcapp.controllers { public class processcontroller : controller { // get: process public string index() { return "processcontroller.index() page."; } // get: process public string list() { return "processcontroller.list() page."; } } }
第6步 - 当运行这个应用程序,会看到默认路由的结果。当指定以下url( http://localhost:56922/process/list )时,您将看到processcontroller的结果。