0.摘要
这里总结了几种MVC中的跳转方式,主要汇总了前端页面跳转,后台的跳转,和过滤器中的跳转方式。
1.在前端页面的跳转方式
<!--属性形式---> <a href="@Url.Action("Index","Test",new{p1=1,p2=2})">跳转到Test</a> <!--标签形式---> @Html.ActionLink("跳转到Test", "Index", "Test", new { p1 = 1, p2 = 2 }, null)
2.在后台通过ActionResult跳转
public ActionResult GetRedirect(int id) { //return RedirectToAction("BigContent","Test"); //return RedirectToAction("SmallContent","Test",new { name="hello"}); return RedirectToRoute(new { controller = "Home", action = "Index", id = 1,name = "刘备" }); }
3.过滤器中的跳转
如果在Filter中用Response.Redirect,虽然URL是跳转了,但是之后的Filter和Action还是会执行。
1 public override void OnActionExecuting(ActionExecutingContext filterContext) 2 { 3 if (!hasAuthority) 4 { 5 filterContext.Result = new RedirectResult("/Home/Index"); 6 } 7 base.OnActionExecuting(filterContext); 8 }