2012-01-08 75 views
0

也許我不明白asp mvc路由的真正目的。
我創建了一個應用程序,現在我需要修復我的網址a以便更容易理解。
例如,我有控制器Home和動作Index的區域Cities
所以在這裏我需要像這樣的網址:localhost/London但目前的路由我得到localhost/cityPage/Home
我的問題是我可以以某種方式傳遞像city name這樣的參數並使URL像我想要的那樣?
這是我的Global.asax當前默認路由如何控制路由值

routes.MapRoute(
       "Default", 
       "{area}/{controller}/{action}/{id}", 
       new { area = "CityPage", controller = "Home", action = "Index", id = "" }, 
       new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities"); 

新的路由:

routes.MapRoute(null, 
          "CityPage/{cityName}", 
          new 
          { 
           area = "CityPage", 
           controller = "Home", 
           action = "Index" 
          } 
         ); 

      routes.MapRoute(
       "Default", 
       "{area}/{controller}/{action}/{id}", 
       new { area = "CityPage", controller = "Home", action = "Index", id = "" }, 
       new string[] { "MyProject.WebUI.Areas.CityPage.Controllers" }).DataTokens.Add("area", "CityPage"); 

鏈接的例子,我點擊

@Html.ActionLink("City London", "Index", "Home", new { cityName = "London" }, null) 

回答

1

爲了路由URL本地主機/倫敦到城市地區HomeController的索引行動,你需要這樣的路線:

routes.MapRoute(null, 
    "{id}", 
    new 
    { 
     area = "Cities", controller = "Home", action = "Index" 
    } 
); 

確保此路線在CitiesAreaRegistration.cs類中的「默認」路線之前聲明。

但是,如果您的應用程序中有很多其他路線,那麼添加這樣的一般路線可能會對應用程序中的其他路線造成嚴重破壞。我建議將URL添加前綴從別人這條路在你的應用程序分開:

routes.MapRoute(null, 
    "cities/{id}", 
    new 
    { 
     area = "Cities", controller = "Home", action = "Index" 
    } 
); 

這會讓你的URL看起來像本地主機/城市/倫敦。這可以接受嗎?

更新1

除非你徹底刪除你的「默認」路線定義,你會真正有映射到該動作的多個呼入路由。您將有localhost/cities/London,localhost/cityPage/Home,localhost/cityPage/Home/Indexlocalhost/cityPage/Home/Index/London全部解決該操作。但是,當MVC選擇生成OUTBOUND路線時,它會選擇第一個 - localhost/cities/London。

更新2

如果你希望你的路線參數是的cityName,你可以這樣做:

routes.MapRoute(null, 
    "cities/{cityName}", 
    new 
    { 
     area = "Cities", controller = "Home", action = "Index" 
    } 
); 

然而,你將不得不改變你的城市地區的HomeController中的索引操作有這個簽名:

public ActionResult Index(string cityName) 

通過改變參數從id到cityName,你告訴MVC通過這個網址參數/路線段到操作方法。

更新3

是您的區域 「城市」 或 「CityPage」 的名字?根據之前的代碼,它看起來像您所在地區的名稱是Cities。

如果是CitiesPage,嘗試一下本作的操作方法:

@Html.ActionLink("City London", "Index", "Home", 
    new { area = "CityPage", cityName = "London" }) 

最終答案

我只是轉載此在MVC3項目,並正在按預期:

  1. 創建了一個名爲「CityPage」的新區域
  2. 添加了一個帶有索引ac的HomeController給CityPage區域
  3. 爲CityPage/Views/Home文件夾添加了一個索引視圖。

CityPageAreaRegistration.cs:

public class CityPageAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "CityPage"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(null, 
      "CityPage/{cityName}", 
      new { area = "CityPage", controller = "Home", action = "Index" } 
     ); 

     //context.MapRoute(
     // "CityPage_default", 
     // "CityPage/{controller}/{action}/{id}", 
     // new { action = "Index", id = UrlParameter.Optional } 
     //); 
    } 
} 

HomeController.cs:

public class HomeController : Controller 
{ 
    // 
    // GET: /CityPage/Home/ 

    public ActionResult Index(string cityName) 
    { 
     return View(); 
    } 

} 

Index.cshtml:

@{ 
    ViewBag.Title = "Index"; 
} 
<h2> 
    Index</h2> 
@Html.ActionLink("City London", "Index", "Home", 
    new { area = "CityPage", cityName = "London" }, null) 

最後,這裏是由操作鏈接生成的鏈接:

<a href="/CityPage/London">City London</a> 
+0

我加入這個默認之前,但URL不瓚ged :(我如何控制{id}參數? – 1110 2012-01-08 15:02:07

+0

請將您想要控制的{id}參數添加到您的問題中。 – danludwig 2012-01-08 15:12:53

+0

我添加了我的家庭/索引操作方法代碼,也許我犯了一些錯誤。 – 1110 2012-01-08 15:17:35

0

是的,你可以做到這一點的方式,但你必須做以下的事情

  1. 確保您的路線通用路由之前必須註冊。
  2. 獲取有關RouteConstraint

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs

剛例如嘗試這種方式檢查您請求的網址本地主機/倫敦

routes.MapRoute(
       "Default", 
       "{id}", 
       new { area = "CityPage", controller = "Home", action = "Index", id = "" }, 
       new string[] { "MyProject.Areas.Cities.Controllers" }).DataTokens.Add("area", "Cities");