1

我有一些在多个控制器操作开始时运行的通用代码。我想将该代码重构为一个静态类,以促进该代码块的重用。

代码检查变量,查找 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);
                }
            }
        }
    }
}
4

1 回答 1

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
}

或者如果你想全局应用这个(小心这个):在你的 Application 类(继承的那个System.Web.HttpApplication,你可以这样做:

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

        // register routes
    }
}
于 2012-10-26T23:06:31.640 回答