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();
    }

}

{id} パラメーターを必要としないルートにアクセスしようとすると {id} パラメーターを探し続け、その逆も同様であるため、URL ルーティングには問題があるようです。

セットアップに深刻なオーバーラップが見られるか、それとも完全に何かが欠けていますか?

編集

私が使用する 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

制約の代わりに、たとえば、適切な制約を見つけるのが難しい場合、実行できることの1つは、混合セグメントを使用することです。つまり、それらの1つにプレフィックスを追加します。たとえば、次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 に答える