我有一个项目,我希望能够代表以下不同类型的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如下:
答案 0 :(得分:2)
注意一些事项。
/Poll/section/1234
和/Poll/section/organization/
时无法理解哪条路线是正确的。结果您的路线配置应如下所示
routes.MapRoute(
"SectionOrganizationMemberKey", // Route name
"{controller}/{section}/{organization}/{id}/{key}", // URL with parameters
new { controller = "Poll", action = "SectionOrganizationMemberKey" } // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMember", // Route name
"{controller}/{section}/{organization}/{id}", // URL with parameters
new { controller = "Poll", action = "SectionOrganizationMember" }, // Parameter defaults
new { id = @"\d+" }
);
routes.MapRoute(
"SectionMemberKey", // Route name
"{controller}/{section}/{id}/{key}", // URL with parameters
new { controller = "Poll", action = "SectionMemberKey" } // Parameter defaults
);
routes.MapRoute(
"SectionMember", // Route name
"{controller}/{section}/{id}", // URL with parameters
new { controller = "Poll", action = "SectionMember" }, // Parameter defaults
new { id = @"\d+" }
);
routes.MapRoute(
"SectionOrganization", // Route name
"{controller}/{section}/{organization}", // URL with parameters
new { controller = "Poll", action = "SectionOrganization" }
);
routes.MapRoute(
"Section", // Route name
"{controller}/{section}", // URL with parameters
new { controller = "Poll", action = "Section" } // Parameter defaults
);
我已经测试过,工作正常。
答案 1 :(得分:0)
尝试这个
routes.MapRoute(
"Section", // Route name
"{controller}/{ action }", // URL with parameters
new {
controller = "Poll",
action = "Section"
} // Parameter defaults
);
routes.MapRoute(
"SectionMember", // Route name
"{controller}/{ action }/{id}", // URL with parameters
new {
controller = "Poll",
action = "SectionMember",
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganization", // Route name
"{controller}/{ action }/{organization}", // URL with parameters
new {
controller = "Poll",
action = "SectionOrganization",
organization= UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMember", // Route name
"{controller}/{ action }/{organization}/{id}", // URL with parameters
new {
controller = "Poll",
action = "SectionOrganizationMember",
organization= UrlParameter.Optional ,
id = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionMemberKey", // Route name
"{controller}/{ action }/{id}/{key}", // URL with parameters
new {
controller = "Poll",
action = "SectionMemberKey",
id = UrlParameter.Optional,
key = UrlParameter.Optional
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMemberKey", // Route name
"{controller}/{action }/{organization}/{id}/{key}", // URL with parameters
new {
controller = "Poll",
action = "SectionOrganizationMemberKey",
organization= UrlParameter.Optional ,
id = UrlParameter.Optional,
key = UrlParameter.Optional
} // Parameter defaults
);
答案 2 :(得分:0)
作为约束的替代方案,比方说,如果难以找到合适的约束,您可以做的一件事就是使用混合段,即为其中一个添加一些前缀,例如,使用member-{id}
代替{id}
:
routes.MapRoute(
"Section", // Route name
"{controller}/{section}", // URL with parameters
new
{
controller = "Poll",
action = "Section",
} // Parameter defaults
);
routes.MapRoute(
"SectionMember", // Route name
"{controller}/{section}/member-{id}", // URL with parameters
new
{
controller = "Poll",
action = "SectionMember",
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganization", // Route name
"{controller}/{section}/{organization}", // URL with parameters
new
{
controller = "Poll",
action = "SectionOrganization",
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMember", // Route name
"{controller}/{section}/{organization}/member-{id}", // URL with parameters
new
{
controller = "Poll",
action = "SectionOrganizationMember",
} // Parameter defaults
);
routes.MapRoute(
"SectionMemberKey", // Route name
"{controller}/{section}/member-{id}/{key}", // URL with parameters
new
{
controller = "Poll",
action = "SectionMemberKey",
} // Parameter defaults
);
routes.MapRoute(
"SectionOrganizationMemberKey", // Route name
"{controller}/{section}/{organization}/member-{id}/{key}", // URL with parameters
new
{
controller = "Poll",
action = "SectionOrganizationMemberKey",
} // Parameter defaults
);
显然,您将获得以下路线: