0

私はstackoverflowの質問に似たフレンドリーなURLシステムを設定しました。

古いURL構文は次のとおりです。localhost:12345:/ cars / details / 1234

返される301とURL生成をすでに設定しましたが、URLが次の場所にリダイレクトされたときにファイルを取得してもエラーは発生しません。

localhost:12345 / cars / details / 1234 / blue-subaru(最後の「blue-subaru」のため)

もちろん、私は実際に欲しいです:localhost:12345 / cars / 1234 / blue-subaru :)

どうすればこれを達成できますか?ありがとうございました

4

2 に答える 2

3

これはルーティングの問題なので、このようにルーティングを少し変更する必要があります

routes.MapRoute(
               "Default", // Route nameRegister
               "{controller}/{action}/{id}/{name}", // URL with parameters
               new { controller = "test", action = "Index", id = UrlParameter.Optional,name = UrlParameter.Optional } // Parameter defaults
           );

これが役立つと思います。

于 2013-01-10T11:04:20.937 に答える
2

global.asax の RouteTable で車の名前を受け入れるようにルートを構成できます。

routes.MapRoute( 
    "Cars", 
    "Car/{id}/{carName}", 
    new { controller = "Car", action = "Details", id =  UrlParameter.Optional, carName =  UrlParameter.Optional } 
);

そして、CarController詳細アクション メソッドを使用して、両方のパラメーター (id と carName) を取得できます。

public ActionResult Details(int? id, string carName) 
{ 
   var model = /* create you model */

   return View(model);
}

アクション リンクは次のようになります。

@Html.ActionLink("Text", "Details", "Car", new { id = 1, carName="Honda-Civic-2013" })
于 2013-01-10T11:06:23.547 に答える