在本次教程里,你将学到如何在asp.net mvc应用程序里添加一个自定义的路由。你也将学会如何在Global.asax里用自定义的路由去修改默认路由表。
在许多asp.net mvc应用程序里,默认路由表可以很好的工作。然而,你可能已经发现到你需要一个特殊的路由。在这种情况下,你可以创建一个自定义的路由。
想象一下,例如,你正在建立一个博客应用程序。你可能想要处理一个进来的像下面这样的http请求:
/Archive/12-25-2009
当一个使用者输入这个请求后,你想返回一张对应于12/25/2009德博客网页给他,为了处理这类请求,你需要创建一个自定义路由。
下面的Global.asax文件包含一个新的自定义的路由(叫 Blog),它会处理像这样( /Archive/ entry date)的请求。
Listing 1 – Global.asax (with custom route)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System.Web.Mvc;
2
using System.Web.Routing;
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace MvcApplication1
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public class MvcApplication : System.Web.HttpApplication
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
public static void RegisterRoutes(RouteCollection routes)
9![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
11
routes.MapRoute( "Blog", // Route name
12
"Archive/{entryDate}", // URL with parameters
13![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ controller = "Archive", action = "Entry" } // Parameter defaults
14
);
15
routes.MapRoute( "Default",// Route name
16
"{controller}/{action}/{id}", // URL with parameters
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ controller = "Home", action = "Index", id = "" } // Parameter defaults
18
);
19
}
20
protected void Application_Start()
21![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
RegisterRoutes(RouteTable.Routes);
23
}
24
}
25
}
你添加到路由表里的路由命令是重要的。我们自定义的Blog路由会在存在的默认路由之前被添加上去。如果你 颠倒这个命令,那么默认路由将总是被调用,而自定义路由不会被调用。
自定义的路由总是匹配以/Archive/开头的任意请求,所以,它会匹配下面所有的请求:
你可以通过下面的控制器使用Blog路由:
Listing 2 – ArchiveController.cs
Code
using System;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class ArchiveController : Controller
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public string Entry(DateTime entryDate)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "You requested the entry from " + entryDate.ToString();
}
}
}
注意到Entry()方法接受一个datetime类型的参数。而mvc framework已经很完美了,它会自动地把从URL得来的日期字符串转变为datetime类型。
如果从URL得来的日期字符串没能成功地转换为datetime类型,错误就会发生(figure 1)
Figure 1 – Error from converting parameter
在许多asp.net mvc应用程序里,默认路由表可以很好的工作。然而,你可能已经发现到你需要一个特殊的路由。在这种情况下,你可以创建一个自定义的路由。
想象一下,例如,你正在建立一个博客应用程序。你可能想要处理一个进来的像下面这样的http请求:
/Archive/12-25-2009
当一个使用者输入这个请求后,你想返回一张对应于12/25/2009德博客网页给他,为了处理这类请求,你需要创建一个自定义路由。
下面的Global.asax文件包含一个新的自定义的路由(叫 Blog),它会处理像这样( /Archive/ entry date)的请求。
Listing 1 – Global.asax (with custom route)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
你添加到路由表里的路由命令是重要的。我们自定义的Blog路由会在存在的默认路由之前被添加上去。如果你 颠倒这个命令,那么默认路由将总是被调用,而自定义路由不会被调用。
自定义的路由总是匹配以/Archive/开头的任意请求,所以,它会匹配下面所有的请求:
/Archive/12-25-2009
/Archive/10-6-2004
/Archive/apple
自定义路由引导进来的请求到一个叫Archive 的控制器,并调用Entry()动作。当Entry动作被调用。输入的日期将被作为一个叫entryDate的参数传递过去。你可以通过下面的控制器使用Blog路由:
Listing 2 – ArchiveController.cs
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
如果从URL得来的日期字符串没能成功地转换为datetime类型,错误就会发生(figure 1)
Figure 1 – Error from converting parameter
![](https://i-blog.csdnimg.cn/blog_migrate/9fc8bd64b3433494afb6d56683734457.png)
Summary
本次教程的目标是展现如何创建一个自定义的路由。你学会了怎样去添加一个自定义的路由到Global.asax(博客的入口)里的路由表里。
我们讨论了如何引导博客入口点的请求到叫 ArchiveController的控制器何叫 Entry()的动作上。