MVC的基類

來源:互聯網
上載者:User

標籤:mvc

設計一個驗證使用者身份是否登陸的基類BaseController

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

/// <summary>    /// 所有需要進行登入控制的控制器基類    /// </summary>    public class BaseController : Controller     {        /// <summary>        /// 當前登入的使用者屬性        /// </summary>        public UserInfo CurrentUserInfo { get; set; }        /// <summary>        /// 重新基類在Action執行之前的事情        /// </summary>        /// <param name="filterContext">重寫方法的參數</param>        protected override void OnActionExecuting(ActionExecutingContext filterContext)        {            base.OnActionExecuting(filterContext);            //得到使用者登入的資訊            CurrentUserInfo = Session["UserInfo"] as UserInfo;            //判斷使用者是否為空白            if (CurrentUserInfo == null)            {                Response.Redirect("/Login/Index");            }        }        protected override void OnException(ExceptionContext filterContext)        {            base.OnException(filterContext);            //錯誤記錄            WHC.Framework.Commons.LogTextHelper.Error(filterContext.Exception);            // 當自訂顯示錯誤 mode = On,顯示友好錯誤頁面            if (filterContext.HttpContext.IsCustomErrorEnabled)            {                filterContext.ExceptionHandled = true;                this.View("Error").ExecuteResult(this.ControllerContext);            }        }........................    }

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

有了這個基類,我們在首頁的Home控制類,就可以使用使用者資訊對象了進行操作了,而且必須要求客戶登陸了

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />

public class HomeController : BaseController    {        public ActionResult Index()        {            if (CurrentUserInfo != null)            {                ViewBag.FullName = CurrentUserInfo.FullName;                ViewBag.Name = CurrentUserInfo.Name;            }            return View();        }................    }

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />


MVC的基類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.