前言
c# 的mvc 有很多的action跳轉方式,在此總結一下具體的跳轉方法。
這裏不用去記,而是知道能怎麼做,爲什麼能這樣做,重在思考。
正文
一、RedirectToAction("Index");//一個參數時在本Controller下,不傳入參數。
二、RedirectToAction(ActionName,ControllerName) //可以直接跳到別的Controller.
三、RedirectToRoute(new {controller="Home",action="Index"});//可跳到其他controller
四、RedirectToRoute(new {controller="Home",action="Index", id=param});//可跳到其他controller,帶參數。
五、Response.Redirect("Index?id=1");//適用於本controller下的方法名稱,可帶參數。
六、return Redirect("Index");//適用於本controller下的方法名稱。
七、return View("Index"); //直接顯示對應的頁面 不經過執行Controller的方法。
八、return View("~/Views/Home/Index.aspx");//這種方法是寫全路徑,直接顯示頁面,不經過Controller方法
九、return View();//直接顯示頁面,不經過Controller方法