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 に答える