2013-04-05 84 views
12

我正在使用ASP.NET MVC 4,並且我的一些路由設置有問題。你能告訴我如何設置我的路由指向的網址行動如下:在ASP.NET MVC中使用和不使用控制器名稱的路由4

  • 「/」(或 「/啓動」)=> PublicController.Start()
  • 「/關於」=> PublicController.About()
  • 「/我的編目」(或 「/我的編目/概述」)=> MyPageController.Summary()
  • 「/我的編目/發票」=> MyPageController.Invoices()
  • 「/我的頁面/ Invoice/72「=> MyPageController.Invoice(int id)

這是「/ About」這個URL,它爲我搞亂了一些東西,例如一個沒有指定控制器的url。如果我讓那個人工作,其他人確定控制器停止工作。我想我可以爲「/ About」創建一個單獨的控制器,但是如果我不必(我有更多的URL遵循該模式),我寧可不要。

回答

28

這應做到:

routes.MapRoute(
    name: "About", 
    url: "About", 
    defaults: new { controller = "Public", action = "About" } 
); 

routes.MapRoute(
    name: "MyPageSummary", 
    url: "MyPage", 
    defaults: new { controller = "MyPage", action = "Summary" } 
); 

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Public", action = "Start", id = UrlParameter.Optional } 
);