2012-01-08 6 views
0

多分私はasp mvcルーティングの本当の目的を理解していません。
私はアプリケーションを作成しましたが、私は私のURLを理解しやすくする必要があります。
たとえば、コントローラーHomeとアクションIndexの領域Citiesがあります。
ここに私のようなURLが必要です: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"); 

新しいルーティングでは私の現在のデフォルトのルーティングです:/ URLのlocalhostを、私はルートにするために

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

答えて

1

をクリックし、リンクの

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

例ロンドンのCityControlのHomeControllerのIndexアクションには、次のようなルートが必要です。

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

CitiesAreaRegistration.csクラスの "Default"ルートの前にこのルートが宣言されていることを確認してください。

しかし、アプリケーションに他のルートがたくさんある場合は、このような一般的なルートを追加すると、アプリ内の他のルートに混乱を招く可能性があります。

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

これは、URLをlocalhost/cities/Londonのように見せるためにURLプレフィックスを追加することをお勧めします。それは受け入れられますか?

アップデート1

あなたは完全にあなたの「デフォルト」ルート定義を削除しない限り、あなたが実際にこのアクションにマップする複数のINBOUNDルートを持っています。 localhost/cities/Londonlocalhost/cityPage/Homelocalhost/cityPage/Home/Index、およびlocalhost/cityPage/Home/Index/Londonのいずれかが該当します。しかし、MVCがOUTBOUNDルートを生成することを選択すると、最初のlocalhost/cities/Londonが選択されます。

アップデート2

あなたのルートパラメータはcityNameになりたい場合は、あなたがこれを行うだろう:

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

あなたが、その後に自分の都市地域のにHomeController上のindexアクションを変更する必要がありますが

public ActionResult Index(string cityName) 

引数をidからcityNameに変更すると、これを渡すようMVCに指示します。アクションメソッドへのURLパラメータ/ルートセグメント。

アップデート3

は、お住まいの地域 "都市" の名前または "CityPage" となっていますか?以前のコードからは、あなたのエリアの名前が都市だったように見えました。それはCitiesPageであれば、あなたのアクションメソッドのためにこれを試してください

:予想通り

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

最終的な答え

私はMVC3プロジェクトでこれを再現し、それが働いている:

  1. "CityPage"という名前の新しい領域を作成しました
  2. インデックスを持つHomeControllerを追加しましたac 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

+0

{id}パラメータをどのように制御するかを質問に追加してください。 – danludwig

+0

私のhome/indexアクションメソッドコードを追加しました。 – 1110

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

に関する情報を取得

ただ、例えばこの方法を試してみて確認してください、あなたに必要なURLのlocalhostの/ロンドン

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