2012-10-26 13 views
1

我有一些在多個控制器動作開始時運行的通用代碼。我想將該代碼重構爲靜態類以促進該代碼塊的重用。如何在MVC控制器的助手類中執行重定向?

代碼檢查變量,查找cookie,如果符合條件,代碼應該重定向到另一個頁面(控制器/操作)。

問題是,一切正常(包括cookie查找),但重定向不會觸發。代碼通過重定向並且重定向從不發生。

什麼是在輔助類中重定向的正確方法?

這裏是現在的代碼:

這一行是不工作:myController.HttpContext.Response.Redirect(的redirectUrl);

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace MyProject.WebUI 
{ 
    public static class SessionValidationHelper 
    { 
     // helper class to encapsulate common routines to check for missing session data 
     public static void SessionIdRequired(string id, Controller myController) 
     { 
      if (id == null || id == "") 
      { 
       // check cookie 
       if (myController.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("cookiename")) 
       { 
        // if a session cookie was found, send to the registration recovery page 
        string sessionGuidCookieValue = ""; 
        sessionGuidCookieValue = myController.ControllerContext.HttpContext.Request.Cookies["cookiename"].Value; 

        // check if GUID/SessionID exists in persistent cache 

        // send to Session Recovery 
        //myController.HttpContext.Response.RedirectToRoute("SessionRecovery", new { Controller = "SessionRecovery", Action = "Index", id = sessionGuidCookieValue }); 
        string redirectURL = @"~/SessionRecovery/Index/" + sessionGuidCookieValue; 

        // this code isn't working 
        myController.HttpContext.Response.Redirect(redirectURL); 
       } 
      } 
     } 
    } 
} 

回答

2

您可以創建一個過濾器的屬性和裝飾用它的行動: (我已經把你的屬性提供的代碼)

public class SessionValidationAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public void OnAuthorization(AuthorizationContext filterContext) 
    { 
     if (filterContext.Result == null) 
     { 
      var id = filterContext.RouteData.Values["id"] as string; 
      if (id == null || id == "") 
      { 
      // check cookie 
      if (filterContext.Controller.ControllerContext 
       .HttpContext.Request.Cookies.AllKeys 
       .Contains("cookiename")) 
      { 
       // if a session cookie was found, 
       // send to the registration recovery page 
       string sessionGuidCookieValue = ""; 
       sessionGuidCookieValue = filterContext.Controller 
        .ControllerContext.HttpContext.Request 
        .Cookies["cookiename"].Value; 

       // check if GUID/SessionID exists in persistent cache 

       // send to Session Recovery 
       string redirectURL = @"~/SessionRecovery/Index/" 
         + sessionGuidCookieValue; 

       // this code isn't working 
       filterContext.Result = new RedirectResult(redirectURL); 
      } 
      } 
     } 
    } 

    public abstract bool CanAccessResource(User user); 
} 

並在您的行動你這樣做:

[SessionValidationAttribute] 
public ActionResult MyAction() 
{ 
    // code of the action 
} 

或者,如果你想將它應用到類中的所有動作:

[SessionValidationAttribute] 
public class MyController : Controller 
{ 
    // code of the class, containing all actions 
} 

或者,如果你想在全球範圍內應用此(要小心本): 在您的應用程序類(一個繼承System.Web.HttpApplication,你可以這樣做:

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     GlobalFilters.Filters.Add(new SessionValidationAttribute()); 

     // register routes 
    } 
} 
+0

這是一個偉大的答案。實際上比第一個答案更完整一點,因爲var id = filterContext.RouteData.Values [「id」] as string;對我來說是一個關鍵的部分。 –

相關問題