ASP.NET MVC 3中的路由问题

时间:2012-09-04 21:39:03

标签: asp.net-mvc-3 asp.net-mvc-routing

我在global.asax中定义了以下路由:

routes.MapRoute(
               "Agreements2", // Route name
               "Agreements2/{action}/{id}", // URL with parameters
               new { controller = "Agreements", action = "AgreementSummery", id = UrlParameter.Optional } // Parameter defaults
               );
             routes.MapRoute(
                "Agreements", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Agreements", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );

后一条路线运行良好,但我不明白为什么第一条路线不起作用。

协议控制方的AgreementSummery方法定义为:

[HttpGet]
        public ActionResult AgreementSummery(int? id)
        {

            AgreementsModel model = new AgreementsModel();
            model.SelectedAgreementID = id.Value;
            return View(model);
        }

1 个答案:

答案 0 :(得分:1)

id未定义为可选,它被定义为可空。如果为其分配了值

,则为可选项
public ActionResult AgreementSummery(int? id = null)