1、利用View()直接返回视图(不执行Action方法)
public class ScoreController : Controller
{
public ActionResult Index(string name, int age)
{
#region View()的三种写法
return View();//返回Index视图
return View("Create");//返回Create视图
return View("/User/Create");//不能返回User/Create视图,MVC只检查Score文件夹(action所在的控制器)及Share文件夹(模板页)
#endregion
}
}
2、利用Redirect()跳转Action
public class ScoreController : Controller
{
public ActionResult Index(string name, int age)
{
#region Redirect()的四种写法
return Redirect("Index");//进入无参或参数均为可空类型的Index()方法,并开始死循环
return Redirect("Index?age=16");//若Index()存在不可空类型的参数则必须传递参数值,后两项若存在不可空类型的参数可参照此解决方法
return Redirect("Create");//进入无参或参数均为可空类型的Create()方法
return Redirect("/User/Index");//进入无参或参数均为可空类型的User/Index()方法
#endregion
}
}
3、利用RedirectToAction()跳转Action
public class ScoreController : Controller
{
public ActionResult Index(string name, int age)
{
#region RedirectToAction()的四种写法
return RedirectToAction("Index", "Score");//进入无参或参数均为可空类型的Index()方法
return RedirectToAction("Index", "Score", new
{
name = "guo",
age = 16
});//若Index()存在不可空类型的参数则必须传递参数值,后两项若存在不可空类型的参数可参照此解决方法
return RedirectToAction("Create", "Score");//进入无参或参数均为可空类型的Create()方法
return RedirectToAction("Index", "User");//进入无参或参数均为可空类型的User/Index()方法
return RedirectToAction("Index", "User", new { name = "guoguo", age = "18" });//进入无参或参数均为可空类型的User/Index()方法时传递参数
#endregion
}
}
4、通过href进行跳转
前台用href='/Home/Logout'请求,后台使用Redirect()、RedirectToAction()进行控制跳转 。
<a href="/Home/Logout" class="easyui-linkbutton" plain="true" iconCls="icon-power-blue">退出</a>
public class HomeController : Controller
{
public ActionResult Logout()
{
Session.Abandon();
return Redirect("/Login/Index");
//return RedirectToAction("Index", "Login");
}
}
5、通过ajax进行跳转
如果前台使用了ajax发起请求,那就只能在success:function(data){ }中进行页面跳转了,后台写的return View()、return Redirect()、return RedirectToAction()最多只能执行Action,不会跳转页面。
function logout() {
alert("logout()");
$.ajax({
type: "post",
url: "/Home/Logout",
success: function (data) {
//window.location.href = '/Login/Index';
window.location.href = '@Url.Action("Index", "Login")';
},
error: function (err) { }
});
}
public class HomeController : Controller
{
public ActionResult Logout()
{
Session.Abandon();
return Redirect("/Login/Index");
//return RedirectToAction("Index", "Login");
//return View("/Login/Index");
}
}
6、Core框架跨Area重定向
Statrup.cs中的路由配置
app.UseMvc(routes =>
{
routes.MapAreaRoute(
name: "areasSys",
areaName: "Sys",
template: "Sys/{controller=Home}/{action=Index}/{id?}"
);
routes.MapAreaRoute(
name: "areasPersonalAccount",
areaName: "PersonalAccount",
template: "PersonalAccount/{controller=Home}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Home控制器的Index页及后台方法(最外层Home重定向到Sys区域)
<div class="row">
<div class="col-md-3">
<a href="/Home/SkipToAreaSys">跳转到AreaSys</a>
</div>
</div>
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult SkipToAreaSys()
{
//[跨Area重定向方法1]
return Redirect("/Sys/Sys_Dictionary/Index");
}
}
Sys_Dictionary控制器的Index页及后台方法(Sys区域重定向到PersonalAccount区域)
<div class="row">
<div class="col-md-3">
<a href="/Sys/Sys_Dictionary/SkipToAreaPersonalAccount">跳转到AreaPersonalAccount</a>
</div>
</div>
public class Sys_DictionaryController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult SkipToAreaPersonalAccount()
{
//[跨Area重定向方法1]
return Redirect("/PersonalAccount/PersonalAccount_Apply/Index");
}
}
PersonalAccount_Apply控制器的Index页及后台方法(PersonalAccount区域重定向到最外层Home)
<div class="row">
<div class="col-md-3">
<a href="/PersonalAccount/PersonalAccount_Apply/SkipToHome">跳转到Home</a>
</div>
</div>
public class PersonalAccount_ApplyController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult SkipToHome()
{
//[跨Area重定向方法1]
return Redirect("/Home/Index");
}
}