2011-05-23 98 views
2

我目前的網址是這樣的=>http://localhost:4330/Restaurants/?Location=Manchester&Cuisine=0&NetProfit=0&Turnover=0&MaxPrice=120000&SortPriceBy=Low&Page=0URL路由3

我希望它做這樣的事情=>http://localhost:4330/Restaurants/Manchester/?Cuisine=Chinese&MaxPrice=120000

哪裏,這並不具有價值帕拉姆查詢字符串(0)不會包含在查詢字符串中URL 可能嗎?

+0

如果你想隱藏某些參數顯示爲查詢字符串,那麼你可以通過其他方法,即表單值傳遞它們。 – melaos 2011-05-23 09:01:23

回答

1

嘗試增加添加相應的路線:

routes.MapRoute(
    "Restaurants", 
    "Restaurants/{city}", 
    new { controller = "Restaurants", action = "Index", city = UrlParameter.Optional } 
); 

這將映射到Index動作Restaurants控制器上:

public ActionResult Index(string city) 
{ 
    ... 
} 
5

修訂

stringAdd這Global.asax中路線

  routes.MapRoute(
       "Name of route", // Route name 
       "Restaurants/{cityid}/", // URL with parameters 
       new { controller = "Restaurants", action = "Index" } // Parameter defaults 
      ); 

這是控制器:

public ActionResult Index(string city, int cuisine = 0, int ChineseMaxPrice=0) 
{ 
    Return View(); 
} 

如int美食= 0 - 這個設置的默認值,如果沒有這個參數,在查詢字符串

串城市設置參數 - 是這應該是一個參數在字符串中(不是可選的)

+0

這不是雙通配符嗎?它匹配任何有任何動作的控制器..?或者是你在指定具體控制器和操作的'new'子句? – 2011-05-23 09:02:23

+0

對不起,我犯了一個小錯誤/更新。這是額外的路線 – 2011-05-23 09:04:39