1、Controller中的跳转

(1)直接Redirect后加(Controller/Action):Response.Redirect("/Home/Index");  

(2)直接return后加(Controller/Action):return Redirect("/Home/Index");  

(3)使用RedirectToAction方法

        [1]同一个Controller中,直接跳到一个action:return RedirectToAction("Index");  

        [2]不在同一Controller中:return RedirectToAction("Index","Home");

2、基类Controller的登录验证

public class ControllerBase : Controller
    {
        /// <summary>
        /// 重写控制器初始化
        /// </summary>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (Session["UserID"] == null)
            {
                //验证是否登陆后跳转
                Response.Redirect("~/Home/Login");
            }
        }

        /// <summary>
        /// 使用FilterAction,重写其
        /// </summary>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //加载已登录用户对象和Style数据, 由子类实现
            if (Session["UserID"] == null)
            {
                filterContext.Result = RedirectToRoute("Default", new { Controller = "Home", Action = "Login" });
                return;
            }
        }
    }
View Code

相关文章:

  • 2022-02-18
  • 2022-01-15
  • 2021-08-18
  • 2021-04-08
猜你喜欢
  • 2021-12-22
  • 2021-11-06
  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
相关资源
相似解决方案