我在SurveyController中有两个方法:
public ActionResult Index(string surveyId, string surveyWaveId, string programId)
和
public ActionResult Index(string hash)
我希望能够去:
调查/索引/ {string} 或调查/索引/ {string} / {string} / {string}
并根据提供的参数数量路由到正确的操作。 MVC5可以实现吗?我在RouteConfig中也有这个。
routes.MapRoute(
name: "SurveyEmailLink",
url: "Survey/Index/{hash}",
defaults: new { controller = "Survey", action = "Index"},
namespaces: new[] { "Cobalt.Controllers" }
);
routes.MapRoute(
name: "SurveyIconLink",
url: "Survey/Index/{surveyId}/{surveyWaveId}/{programId}",
defaults: new { controller = "Survey", action = "Index" },
namespaces: new[] { "Cobalt.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Cobalt.Controllers" }
);
提前致谢!
答案 0 :(得分:4)
最好(也是最简单)的选项是将你的行为命名为更多地说明它们的用途......除非Index
确实是这两者的恰当名称。
routes.MapRoute(
name: "SurveyIconLink",
url: "Survey/Icon/{surveyId}/{surveyWaveId}/{programId}",
defaults: new { controller = "Survey", action = "Index" },
namespaces: new[] { "Cobalt.Controllers" }
);
routes.MapRoute(
name: "SurveyEmailLink",
url: "Survey/Email/{hash}",
defaults: new { controller = "Survey", action = "Index"},
namespaces: new[] { "Cobalt.Controllers" }
);
答案 1 :(得分:1)
对于我的具体问题,我只能按照Jonesopolis和Tommy的建议使用RouteAttributes。
我最后添加了
[Route("Survey/View/{surveyId}/{surveyWaveId}/{programId}")]
和
[Route("Survey/View/{hash}")]
高于我的控制器操作,并添加了
routes.MapMvcAttributeRoutes();
到RouteConfig。