2012-07-24 135 views
0

我MVC3應用具有以下網址:http://mysite.com/controller/details?id=56搜索引擎友好的URL路由

我要讓搜索引擎友好像

http://mysite.com/controller/title-of-the-entity-56 

我怎樣才能實現呢?

+0

您可以創建的Global.asax.cs文件的自定義路線。 – 2012-07-24 22:16:56

回答

1

您需要設置新路線,但我強烈建議您重新考慮自己解析實體的ID。我的意思是:

有了這個URL,ID可以被傳遞到操作爲數字數據類型(INT爲例):http://mysite.com/controller/details?id=56

有了這個URL,該ID只能作爲傳遞字符串的一部分:http://mysite.com/controller/title-of-the-entity-56

..這意味着你必須解析字符串,提取數字並將其轉換。

最好瞄準這種格式:http://mysite.com/controller/details/id/title-of-entity,有點StackOverflow如何做到這一點。

嘗試增加這條路線,你的默認路由上面,在Global.asax.cs中:

routes.MapRoute(
      "DetailsRoute", 
      "details/{id}/{entityTitle}", 
      new { controller = "details", action = "Index", id = UrlParameter.Optional } 
     ); 

然後你就可以有這樣的作爲在細節上控制你的索引操作:

public ActionResult Index(int id, string entityTitle) { 

}