【发布时间】:2010-10-08 06:51:33
【问题描述】:
我有默认路由:
routes.MapRoute(
"Shortie", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "Ettan", action = "Index", id = "id" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Ettan", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
我有一个控制器:NewsController。它有一种方法,如下所示:
public ActionResult Index(int id)
{
...
}
如果我浏览到 /News/Index/123,它可以工作。 /新闻/123作品。但是,/News/Index?id=123 没有(它找不到任何名为“index”的方法,其中 id 允许为空)。所以我似乎对路由和模型绑定器如何协同工作缺乏一些了解。
询问的原因是我想要一个包含不同新闻来源的下拉菜单,参数为“id”。所以我可以选择一个新闻源(例如“sport”,id = 123),它应该被路由到我的索引方法。但我似乎无法让它发挥作用。
【问题讨论】:
-
也许你应该这样进行方法签名:Index(int?id) ?
-
是的,我现在用 if (!id.HasValue) { id = int.Parse(Request.QueryString["id"]);但我想了解为什么我需要这样做。
标签: c# asp.net-mvc-2 asp.net-mvc-routing