我有一个项目,我希望能够表示以下不同类型的 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
);
我的 Controller 中有以下代码:
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 如下:
最佳答案
注意一些事项。
- 路由在路由配置中的位置很重要,如果将路由从复杂到简单放置会更好。
- 如果您没有可选 ID,请不要指定它。
- 您应该应用内置路线约束,因为路线系统在从
/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
);
我已经测试过,工作正常。
关于c# - MVC URL 路由复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639209/