2012-11-30 162 views
2

我有一個項目,我希望能夠表示以下不同類型的URL路徑/路由。MVC 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

你能給你可以使用一個URL的例子嗎? – jzm

+0

問題可能在於您創建路線的順序。路由引擎將使用第一條匹配路線。一般來說,你想把最具體的路線放在最上面,而更一般的路線放在最下面。您可能需要玩弄創建路線的順序,以按照您的意圖進行工作。 – HTX9

+0

我已經添加了如上所示的示例URLS – TheJediCowboy

回答

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 
); 
+1

'section'在哪裏? 'section'和'action'在這裏有所不同。 'section'是一個變量,不是動作的名稱 – horgh

+0

如果你看看控制器,Section是控制器上的第一個動作。 – TheJediCowboy

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

我測試,做工精細。

+0

+1:很好的假設,那個'id'只能是數字和大量的約束 – horgh

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

測試它,應該工作的規定