我有一个项目,我希望能够表示以下不同类型的 URL 路径/路由。
{controller}/{section}
{controller}/{section}/{id}
{controller}/{section}/{organization}
{controller}/{section}/{id}/{key}
{controller}/{section}/{organization}/{id}
{controller}/{section}/{organization}/{id}/{key}
我在global.asax中指定了路由映射,如下所示:
routes.MapRoute(
"Section", // Route name
"{controller}/{section}", // URL with parameters
new {
controller = "Poll",
action = "Section",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionMember", // Route name
"{controller}/{section}/{id}", // URL with parameters
new {
controller = "Poll",
action = "SectionMember",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganization", // Route name
"{controller}/{section}/{organization}", // URL with parameters
new {
controller = "Poll",
action = "SectionOrganization",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMember", // Route name
"{controller}/{section}/{organization}/{id}", // URL with parameters
new {
controller = "Poll",
action = "SectionOrganizationMember",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionMemberKey", // Route name
"{controller}/{section}/{id}/{key}", // URL with parameters
new {
controller = "Poll",
action = "SectionMemberKey",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMemberKey", // Route name
"{controller}/{section}/{organization}/{id}/{key}", // URL with parameters
new {
controller = "Poll",
action = "SectionOrganizationMemberKey",
id = UrlParameter.Optional
} // Parameter defaults
);
我的控制器中有以下代码:
public class PollController : Controller {
public ActionResult Section(string section) {
return View();
}
public ActionResult SectionMember(string section, int id) {
return View();
}
public ActionResult SectionOrganization(string section, string organization) {
return View();
}
public ActionResult SectionOrganizationMember(string section, string organization, int id) {
return View();
}
public ActionResult SectionMemberKey(string section, int id, string key) {
return View();
}
public ActionResult SectionOrganizationMemberKey(string section, string organization, int id, string key) {
return View();
}
}
URL 路由似乎有些复杂,因为当我尝试访问不需要的路由时,它会一直寻找 {id} 参数,反之亦然。
我的设置是否显示任何严重的重叠,或者我完全错过了什么?
编辑
我将使用的一些示例 URL 如下: