路由是将HTTP请求定向到控制器的过程,该处理的功能在System.Web.Routing中实现,该程序集不是ASP.NET MVC的一部分,它实际上是ASP.NET运行时的一部分,并且与ASP.NET一起作为.NET 3.5 SP1正式发布。
MVC框架使用 System.Web.Routing ,但ASP.NET动态数据也使用它, MVC框架利用路由将请求定向到控制器, Global.asax文件是应用程序的一部分,您将在其中定义应用程序的路由。
这是我们在上一章中创建的MVC App中Global.asax中应用程序启动事件的代码。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MVCFirstApp { 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 MVCFirstApp { 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参数。
链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp.net-mvc-routing.html
来源:LearnFk无涯教程网
MVC应用程序使用ASP.NET路由系统,该系统决定URL如何映射到控制器和方法。
当Visual Studio创建MVC项目时,它会添加一些默认路由来帮助我们入门,运行您的应用程序时,您会看到Visual Studio已将浏览器定向到端口63664。几乎可以肯定的是,浏览器请求的URL中将看到一个不同的端口号,因为在创建项目时Visual Studio会分配一个随机端口。
在最后一个示例中,我们添加了HomeController,因此您还可以请求以下任何URL,它们将被定向到HomeController上的Index操作。
http://localhost:63664/Home/
http://localhost:63664/Home/Index
当浏览器请求http://mysite /或http://mysite/Home时,它会从HomeController的Index方法获取输出。
您也可以通过在浏览器中更改URL来尝试此操作,在此示例中,它是http://localhost:63664 /,但端口可能不同。
如果将/Home或/Home/Index附加到URL并按回车按钮,您将从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 Controller,我们可以使操作略有不同(默认操作),可以使该操作成为List而不是Index。
现在,到达的请求类似于localhost/process。路由引擎将使用此路由配置进行传递,因此它将使用默认操作List。
以下是完整的类实现。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MVCFirstApp{ public class RouteConfig{ 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: "Default", url: "{controller}/{action}/{id}", defaults: new{ controller = "Home", action = "Index", id = UrlParameter.Optional}); } } }
步骤1 - 运行此程序并请求具有以下URL http://localhost:63664/Process 的进程页面
您将看到一个HTTP 404,因为路由引擎正在寻找ProcessController,它不可用。
步骤2 - 右键单击Solution Explorer中的Controllers文件夹,然后选择Add→Controller,创建ProcessController。
它将显示"Add Scaffold"对话框。
步骤3 - 选择" MVC 5 Controller-Empty"选项,然后单击"Add"按钮。
出现"Add Controller"对话框。
步骤4 - 将名称设置为ProcessController,然后单击"Add"按钮。
现在,您将在Controllers文件夹中看到一个新的C#文件ProcessController.cs,该文件夹也可以在Visual Studio中进行编辑。
现在我们的默认操作将是List,所以我们想在这里使用List操作而不是Index。
步骤5 - 将返回类型从ActionResult更改为string,并使用以下代码从此action方法返回一些字符串。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVCFirstApp.Controllers{ public class ProcessController : Controller{ //GET: Process public string List(){ return "This is Process page"; } } }
步骤6 - 运行此应用程序时,您将再次看到默认路由的输出。当您指定以下URL http://localhost:63664/Process/List时,您将看到来自ProcessController的输出。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)