mvc,超連接除了直接寫在a標籤的href內還能夠使用路由規則來生成,這樣在改變了路由規則或者路由名稱時不用再去代碼中更改href的值,並且還容易遺漏。藉助Url.RouteUrl或者Html.RouteLink就能夠輕鬆實現,再之後的改變中將不用再去理會路由規則的改變而帶來的工做量。html
路由規則:mvc
//bubuko測試 routes.MapRoute("bubuko_1", "{controller}-{action}-{eID}-{pageIndex}.html", new { controller = "abc", action = "Index" }); //bubuko測試 routes.MapRoute("bubuko_2", "{action}-{eID}-{pageIndex}.html", new { controller = "abc", action = "Index" });
cshtml中代碼:測試
Url.RouteUrl方式,路由名稱是「bubuko_1」:@Url.RouteUrl("bubuko_1", new { eID = ViewBag.eID, pageIndex = ViewBag.pageIndex }) <br /> Url.RouteUrl方式,路由名稱是「bubuko_2」:@Url.RouteUrl("bubuko_2", new { eID = ViewBag.eID, pageIndex = ViewBag.pageIndex }) <br /> <br /> Html.RouteLink方式,路由名稱是「bubuko_1」:@Html.RouteLink("bubuko.com分享超連接", "bubuko_1", new { eID = ViewBag.eID, pageIndex = ViewBag.pageIndex }, new { id = "abc" }) <br /> Html.RouteLink方式,路由名稱是「bubuko_2」:@Html.RouteLink("bubuko.com分享超連接", "bubuko_2", new { eID = ViewBag.eID, pageIndex = ViewBag.pageIndex }, new { id = "abc" })
顯示效果,html代碼:htm
Url.RouteUrl方式,路由名稱是「bubuko_1」:/abc-abclist-10-12.html <br /> Url.RouteUrl方式,路由名稱是「bubuko_2」:/abclist-10-12.html <br /> <br /> Html.RouteLink方式,路由名稱是「bubuko_1」:<a href="/abc-abclist-10-12.html" id="abc">bubuko.com分享超連接</a> <br /> Html.RouteLink方式,路由名稱是「bubuko_2」:<a href="/abclist-10-12.html" id="abc">bubuko.com分享超連接</a>
原文:mvc中Url.RouteUrl或者Html.RouteLink實現靈活超連接,使href的值隨路由名稱或配置的改變而改變路由