【发布时间】:2014-08-11 12:12:29
【问题描述】:
在我的RouteConfig 我有以下内容
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Inbox", action = "Index", id = UrlParameter.Optional }
);
}
所以我希望这样的 URL 可以工作。
在我的ActivityController 中,我有一个接受整数作为参数的索引方法。我希望像 localhost/activity/1234 这样的 URL 调用活动控制器的 Index 方法。但是我收到一条错误消息,说所需的 int 参数为 null
如果我将 URL 更改为:localhost/activity/?param=1234 它正在工作。有没有什么方法可以让它在没有参数名称的情况下工作?
【问题讨论】:
-
看起来您在 ActivityController 上的 Index 方法接受了一个名为
param的参数,而不是id -
是的。我认为 {id} 是任何参数的占位符,但名称似乎很重要
标签: asp.net-mvc asp.net-mvc-4 routes asp.net-mvc-routing url-routing