我使用的是默认路由,因此我不需要指定 Controller 。
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
有了这个,我可以创建像 myapp.com/Customers 这样的 URL 而不是 myapp.com/Home/Customers
当我在本地测试时,一切都很好。当我上传实时版本时,使用 Html.ActionLink 生成的任何链接都是空的。我知道我正确地使用了 Html.ActionLink,因为它在本地运行良好:
// Title Action Controller
<%: Html.ActionLink("Manage My Settings", "Settings", "Home") %>
我已经删除了除默认路径之外的所有路径,尝试在有和没有 Controller 的情况下指定 ActionLink 等。我什至尝试恢复到在路径中有 Controller ,例如:
"{controller}/{action}/{id}"
没有任何东西是实时的。一切都在本地工作。有点疯狂。
更新:
好吧,有一个奇怪的发现。我实际上在 id 之后还有另一个可选的 UrlParameter,称为 page。我愚蠢地没有将它包含在示例中,因为我认为它没有任何区别。如果我把它拿出来,事情似乎就可以了。
所以,实际上,这是可行的:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
并且这个有效!
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
但是这确实不有效
routes.MapRoute(
"Default",
"{action}/{id}/{page}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional, page = UrlParameter.Optional }
);
为什么不呢?
最佳答案
找到答案了!当使用两个连续的可选 UrlParameters 时,MVC3 中存在一个错误,Phil Haack 在此处详述了 routing-regression-with-two-consecutive-optional-url-parameters
您需要先声明一个只有一个可选参数的路由版本。所以
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with ONE parameter
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default_with_page", // Route name
"{action}/{id}/{page}", // URL with TWO parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional, page = UrlParameter.Optional }
// Parameter defaults
);
现在看起来真的很明显。如果我真的包含了所有细节,我相信 Serghei 或其他人会发现问题,所以感谢所有帮助的人!
关于asp.net-mvc - ASP.NET MVC : Empty ActionLinks appearing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8911471/