mvc中多參數URL會很長,首次加載不傳參數讓url很短,路由規則實現方法[bubuko.com]

如要實現列表中地址全路徑「bubuko-11-2.html」,在首次進入時,使用短路徑「bubuko.html」,只有再次href後才顯示全路徑「bubuko-11-2.html」,下面使用路由規則來實現。html

 

//"/bubuko-11-2.html",eID=11,pageIndex=2
routes.MapRoute("bubuko", "{action}-{eID}-{pageIndex}.html",
               new { controller = "bubuko", action = "Index" });

//"/bubuko-11.html",eID=11,pageIndex默認是1
routes.MapRoute("bubuko_No_pageIndex", "{action}-{eID}.html",
               new { controller = "bubuko", action = "Index", eID = 1, pageIndex = 1 });

//"/bubuko.html",eID默認是1,pageIndex默認是1
routes.MapRoute("bubuko_No_eID_pageIndex", "{action}.html",
               new { controller = "bubuko", action = "Index", eID = 1, pageIndex = 1 });

 

完整方案:mvc

 

routes.MapRoute("bubuko_wuAction", "{controller}.html", new { controller = "home", action = "index", id = "" });//沒有Action的路由規則
routes.MapRoute("bubuko_wuID", "{controller}/{action}.html", new { controller = "home", action = "index", id = "" });//沒有ID的路由規則
routes.MapRoute("Default", "{controller}/{action}/{id}.html", new { controller = "home", action = "index", id = "" });//默認匹配
routes.MapRoute("Root", "", new { controller = "home", action = "index", id = "" });//根目錄路由規則

 

 原文:mvc中多參數URL會很長,首次加載不傳參數讓url很短,路由規則實現方法url

原地址:http://bubuko.com/infodetail-145230.htmlhtm

相關文章
相關標籤/搜索