我将以下路线添加到我的MVC应用程序中。
routes.MapRoute("leads", "leads/{leadId}", new { controller = "Leads", action = "ViewLead" });
然而,似乎现在任何关于引导控制器的操作方法都会导致它尝试转到ViewLead操作,并且不会将id作为其动作传递。 我有点失望,然后必须将以下内容添加到我的路线中,这样我就不会对我的其他操作产生异常:
routes.MapRoute("leadIndex", "leads", new { controller = "Leads", action = "Index" });
routes.MapRoute("leadAdd", "leads/addlead", new { controller = "Leads", action = "AddLead" });
routes.MapRoute("leadsSearch", "leads/searchleads", new { controller = "Leads", action = "SearchLeads" });
routes.MapRoute("leadsGetAll", "leads/getallleads", new { controller = "Leads", action = "GetAllLeads" });
routes.MapRoute("leadsUpdate", "leads/updatelead", new { controller = "Leads", action = "UpdateLead" });
routes.MapRoute("leadsRecipients", "leads/getrecipients", new { controller = "Leads", action = "GetRecipients" });
我在这里做错了吗?它似乎违反DRY原则,因为我之前没有必要指定这些路线,但现在我做了......
答案 0 :(得分:2)
所有路由与lead / {leadId}路由匹配的原因是因为它匹配{string} / {string}的模式。你需要做的是添加一个约束,表明leadid是一个int。
routes.MapRoute(
"leads",
"leads/{leadId}",
new { controller = "Leads", action = "ViewLead" },
new { leadId = @"\d+" });
这只会匹配第二个位置的参数至少有一个或多个整数时的路径。
但是,如果您想拥有多个具有相同基础的自定义路线,请查看Phil Haack撰写的这篇文章:http://haacked.com/archive/2010/12/02/grouping-routes-part-1.aspx
希望这有帮助!