使用mvc路由约束,因此url只能映射到三个可能的参数之一

Fiv*_*ols 16 c# model-view-controller asp.net-mvc asp.net-mvc-routing asp.net-mvc-3

这是我的路线:

routes.MapRoute(null, "myaccount/monitor/{category}", // Matches
                new { controller = "MyAccount", action = "Monitor", category = (string)null }
);
Run Code Online (Sandbox Code Playgroud)

我想添加一个约束,所以类别只能匹配null或三个参数之一(即概述,投影,历史)

Gab*_*oli 27

您可以使用UrlParameter.Optional允许空值,也可以使用方法constraints参数.MapRoute

 routes.MapRoute(null,
                      "myaccount/monitor/{category}", // Matches
                      new { controller = "MyAccount", action = "Monitor", category = UrlParameter.Optional  },
                      new { category = "overview|projection|history"}
            );
Run Code Online (Sandbox Code Playgroud)