c# - MVC URL 路由复杂性

标签 c# asp.net-mvc asp.net-mvc-routing

我有一个项目,我希望能够表示以下不同类型的 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/

相关文章:

c# - SqlDependency 尝试使用 log4net 执行某些操作并无法启动

c# - 如何运行只有 NotifyIcon 的 "empty"Windows 应用程序?

asp.net-mvc - JSON、ASP.NET MVC - MaxJsonLength 异常

c# - 如何使用 MVC .Net 修复 google insights 上的杠杆浏览器缓存问题

asp.net-mvc - Controller 继承 : how to override controller's action and force using new action when Html. 调用操作

asp.net-mvc - ASP.NET MVC : Uri to usable route data

c# - 只要不被括号括起来,就用 Regex 匹配字符串

c# - 通过 html 敏捷包获取标题标签

asp.net-mvc - 如何使用可选的查询字符串参数测试 MVC 路由

asp.net-mvc - MVC Preview 4 - 路由表中没有路由与提供的值匹配