为同一页面创建两个路由选项(MVC)

时间:2011-11-02 07:44:50

标签: asp.net-mvc-2 routes

我希望能够以两种不同的方式访问同一页面:

首先,使用参数显示一些特定信息。

        routes.MapRoute(
            "About",
            "About/{id}",
            new { controller = "About", action = "Index" }
        );

其次,没有参数,以显示一般事物。

        routes.MapRoute(
            "About",
            "About",
            new { controller = "About", action = "Index" }
        );  

如何建立接受这两种选择的路线?

1 个答案:

答案 0 :(得分:2)

routes.MapRoute(
    "About",
    "About/{id}",
    new { controller = "About", action = "Index", id = UrlParameter.Optional }
);

然后:

public ActionResult Index(string id)
{
    // if id = null => /About was requested
    // if id != null => /About/abc was requested
    ...
}