2

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

4

3 回答 3

2

注意一些项目。

  • 路线配置中路线的位置很重要,如果您将路线从复杂到简单放置会更好。
  • 如果您没有可选的 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
        );

我已经测试过了,工作正常。

于 2012-11-30T06:02:48.673 回答
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
);

显然,您将获得以下路线:

  1. http://mysite.com/Poll/section
  2. http://mysite.com/Poll/section/member-1234
  3. http://mysite.com/Poll/section/organization/
  4. http://mysite.com/Poll/section/member-1234/key
  5. http://mysite.com/Poll/section/organization/member-1234
  6. http://mysite.com/Poll/section/organization/member-1234/key

测试过了,应该可以正常工作

于 2012-11-30T06:05:44.630 回答
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
);
于 2012-11-30T05:40:41.020 回答