2012-01-30 20 views
0

我有一個MVC 3網站中的內容存儲在數據庫中(也叫做CMS)。網站所有者不希望網站在仍在編輯內容時部分運行。如果我使用「app_offline.htm」方法,則無人可以登錄和編輯內容。對此有什麼其他方法?「app_offline.htm」爲MVC 3

回答

0

我通常會創建一個頁保持機制,這將看看如果一個特定的Cookie已設置。在過去,我通過創建一個HttpModule來完成這項工作,但最近我使用了ActionFilter,所以我可以微調哪些控制器需要繼續。一旦Cookie被設置,您可以正常使用您的網站。

public class CookieProtectAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (WebSettings.HoldingPageOn) //Helper to check web.config if holding page is active 
     { //Make sure you don't go into an infinite loop - ideally the holding page controller wont have the cookie protect action filter 
      if (!filterContext.HttpContext.Request.RawUrl.ToLower().Contains("holding")) 
      { 
       var accessCookie = filterContext.HttpContext.Request.Cookies["AllowAccess"]; 
       if (accessCookie == null) 
       { 
        filterContext.Result = new RedirectToRouteResult("Holding", null, false); 
        filterContext.Result.ExecuteResult(filterContext); 
       } 
      } 
     } 

     base.OnActionExecuting(filterContext); 
    } 
} 

然後設置cookie我會連線長達一個操作方法,例如特定的路線......

[HttpGet] 
    public ActionResult HoldingAccess(string id) 
    { 
     if (id.NullSafe() == "yourpassword") 
     { 
      Response.Cookies.Add(new HttpCookie("AllowAccess") {Expires = DateTime.Now.AddDays(7)}); 
      return RedirectToRoute("Home"); 
     } 
     return HttpNotFound(); 
    } 

所以路線,你設置設置cookie可以是任何東西,而在看上面的例子可以設置一個硬編碼「密碼」 - 即/ OPENSESAME /你的密碼路線將設置cookie

很簡單的代碼,但它似乎運作良好。