2017-02-26 61 views
0

我有網站使用Drupal。 現在,我想使用Asp.net MVC來構建一個類似的網站,但我不能像在Drupal中那樣配置路由。如何配置像Drupal一樣的Asp.net 5 MVC路由?

在Drupal的:

  1. /:主頁,默認語言:六
  2. /{語言}:在語言{語言}主頁
  3. /{catePermanentLink}:分類列表新聞文章,默認語言:六
  4. /{語言}/{} catePermanentLink:在語言類的列表新聞文章{}郎
  5. /{catePermanentLink}/{} newsPermanentLink:查看新聞內容,默認語言:六
  6. /{語言}/{catePermanentLink}/{newsPermanentLink}:查看新聞內容,在語言{語言}
  7. /{catePermanentLink_Level1}/{catePermanentLink_Level2}:分類列表新聞文章,默認語言:六
  8. /{lang}/{catePermanentLink_Level1}/{catePermanentLink_Level2}:使用語言{lang}列出新聞類別的文章

如何在Asp.net MVC中進行配置。 非常感謝。

+0

什麼是控制器的方法與每個相關聯,你想讓url看起來像什麼?例如5號,你想要'../ fr/News/Detail/10'(或者也許只是'..fr/Details/10')來顯示'ID = 10'的News項目的細節用法語? –

+0

對於那些不熟悉Drupal的人來說,這個問題相當模糊,但[這個答案](http://stackoverflow.com/a/32839796/181087)可能會讓你朝正確的方向發展。 – NightOwl888

+0

{root}/{catePermanentLink}/{newsPermanentLink} ==>使用默認語言的新聞細節。 {root}/fr/{catePermanentLink}/{newsPermanentLink} ==>使用FR語言的新聞細節 – Linc

回答

0

謝謝大家。我分享我的工作配置

//Ưu tiên Search 
     routes.MapRoute(name: "search", url: "{lang}/search/{keyword}", defaults: new { controller = "search", action = "result" }); 

     //Normal with Language 
     routes.MapRoute(
      name: "Language", 
      url: "{lang}", 
      defaults: new { controller = "Home", action = "Index", lang = UrlParameter.Optional }, 
      constraints: new { lang = @"(\w{2})" } 
     ); 

     //News Category 
     routes.MapRoute(
      name: "defaultLanguageWithCate", 
      url: "{cateSlug}", 
      defaults: new { lang = "vi", controller = "News", action = "NewsByCate" }, 
      constraints: new { lang = @"(\w{2})", controller = "News", action = "NewsByCate" }, 
      namespaces: new[] { "Frontend.Web.Controllers" } 
     ); 

     routes.MapRoute(
      name: "languageWithCate", 
      url: "{lang}/{cateSlug}", 
      defaults: new { lang = "vi", controller = "News", action = "NewsByCate" }, 
      constraints: new { lang = @"(\w{2})", controller = "News", action = "NewsByCate" }, 
      namespaces: new[] { "Frontend.Web.Controllers" } 
     ); 

     //News Detail 
     routes.MapRoute(
      name: "defaultLanguageWithArticle", 
      url: "{cateSlug}/{articleSlug}", 
      defaults: new { lang = "vi", controller = "News", action = "Detail" }, 
      constraints: new { lang = @"(\w{2})", controller = "News", action = "Detail" }, 
      namespaces: new[] { "Frontend.Web.Controllers" } 
     ); 

     routes.MapRoute(
      name: "languageWithArticle", 
      url: "{lang}/{cateSlug}/{articleSlug}", 
      defaults: new { lang = "vi", controller = "News", action = "Detail" }, 
      constraints: new { lang = @"(\w{2})", controller = "News", action = "Detail" }, 
      namespaces: new[] { "Frontend.Web.Controllers" } 
     ); 

     //Không có Language 
     routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, lang = "vi" } 
     );