这里,我们将学习到mvc中一个重要的特性即asp.net routing。这个模块用来把用户的请求映射到特定的mvc controller actions,看完这篇将可以知道标准路由表是怎么把请求映射到controller action的。
使用默认ASP.NET MVC路由表
当创建好一个mvc应用后,应用已经被配置好可以用asp.net routing了。ASP.NET MVC路由在两个地方被激发。
一、asp.net routing在web.config中被激活,有关route的配置有四个配置节:system.web.httpModules , system.web.httpHandlers , system.webserver.modules, the system.webserver.handlers,删除这些配置节时要小心,因为没有它们routing将不再工作。
二、更重要的是,在global.asax文件中已经创建了路由表,global.asax是一个包含了asp.net应用生命周期内事件句柄的特殊文件。路由表在Application Start 事件中被创建。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
当程序运行后,Application_Start() 方法被调用,同时调用方法内的RegisterRoutes() 方法。这个方法创建了路由表。
默认的ASP.NET MVC路由表中只含有一个叫做default的路由。这个默认路由把url拆分成三个部分分别对应controller,action和View。
当请求URL:/Home/Index/3时,下列代码执行:HomeController.Index(3)。
如果不指定controller,则默认为Home,不指定Action则默认为Index,不指定参数则默认为空。
我们将通过例子来看一下到底默认路由是怎么把URl映射到controller和action的。想象我们在地址栏中输入了如下URL:
/Home
由于默认action为Index,所以下面的方法被调用
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(string id)
{
return View();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
这个方法接受一个字符串类型的id参数,上面的URL执行了这个方法,传入一个空的id。
由于mvc框架引用controller actions的方式,/Home同时也会触发下列事件
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
这个方法不接受参数,/Home将触发这个Index(),同时/Home/Index/3也会调用这个方法,id参数将被忽略。
/Home也会与下面的方法匹配
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(int? id)
{
return View();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
该方法接受一个int类型的参数id,因为这个参数可以为null,该方法将被调用而不会产生任何错误。
***调用下面的方法,这里会引发一个异常
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(int id)
{
return View();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
因为这个参数是不可以为null的。以上就介绍了ASP.NET MVC路由的使用方法。
【编辑推荐】