【发布时间】:2011-04-17 19:35:23
【问题描述】:
我正在使用 .NET 3.5、MVC 2 和 T4MVC 2.6.42...
我有以下操作:
public virtual ActionResult Index(string id, int page = 1)
还有以下路线:
routes.MapRoute(
"Products", // Route name
"Products/{id}", // URL with parameters
new { controller = "Products", action = "Index", id = UrlParameter.Optional, page = UrlParameter.Optional }, // Parameter defaults
new string[] { "Web.Controllers" }
);
但是当我尝试调用 MVC.Products.Index("anything") 时,我得到一个“方法 'Index' 没有重载需要 '1' 参数”异常。但是,调用 MVC.Products.Index() 可以。
难道我不能省略“page”参数,因为它默认为“1”吗?
注意:我试过在路由中默认page参数为1,没用。
注意2:也试过[Optional] Attribute没有成功。
【问题讨论】:
-
我们能看到完整的堆栈跟踪吗?
-
@KirkWoll 抱歉,我设法找到了解决方案。显然,可选参数是 C# 4.0 的东西(documentation 状态 VS2010)。奇怪的是,当我在 C# 3.0 中声明这样的方法时,我没有收到编译器错误
标签: c# asp.net-mvc t4mvc